如何解决如何将此Powershell脚本转换为bin / sh脚本?
如何转换此脚本:
NET USE \\65.161.3.129\someFolder\test /u:somedomain\user myPassword123!
robocopy . \\65.161.3.129\someFolder\test /s
NET USE \\65.161.3.129\someFolder\test /d
或带有参数:
Param($mypath)
NET USE $mypath /u:somedomain\user myPassword123!
robocopy . $mypath /s
NET USE $mypath /d
如何制作在Linux /bin/sh
下工作的类似脚本?
我想将文件复制到某个网络位置(Windows共享文件夹)。 Windows服务器上没有scp,我无法安装任何东西。
解决方法
如果您使用的是Linux,则curl
实用程序可以处理SMB(即\\machine\share
)路径。示例:
curl -u "domain\username:passwd" smb://server.example.com/share/file.txt
请参见https://curl.haxx.se/docs/manual.html
,在Linux上,您需要以下文件(在Ubuntu 18.04上进行了验证,尽管没有Windows domain 帐户):
-
先决条件:必须安装
cifsutil
软件包,下面的脚本可以确保此软件包(按需调用sudo apt-get install cifs-utils
)。 -
选择一个(临时)安装点(一个本地目录,通过该目录可以访问共享文件)。
-
使用
mount.cifs
实用程序安装共享,然后使用umount
卸载(删除)共享。 -
使用
cp -R
复制目录层次结构。
注意:
-
sudo
(管理)特权是必需的;该脚本将提示一次输入密码,该密码通常会缓存几分钟。
#!/bin/sh
# The SMB file-share path given as an argument.
local mypath=$1
# Choose a (temporary) mount-point dir.
local mountpoint="/tmp/mp_$$"
# Prerequisite:
# Make sure that cifs-utils are installed.
which mount.cifs >/dev/null || sudo apt-get install cifs-utils || exit
# Create the (temporary) mount-point dir.
sudo mkdir -p "$mountpoint" || exit
# Mount the CIFS (SMB) share:
# CAVEAT: OBVIOUSLY,HARD-CODING A PASSWORD IS A SECURITY RISK.
sudo mount.cifs -o user= "user=user,pass=myPassword123!,domain=somedomain" "$mypath" "$mountpoint" || exit
# Perform the copy operation
# Remove the `echo` to actually perform copying.
echo cp -R . "$mountpoint/"
# Unmount the share.
sudo umount "$mountpoint" || exit
# Remove the mount-point dir.,if desired
sudo rmdir "$mountpoint"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。