SMB 客户端命令通过 Shell 脚本

SMB Client Commands Through Shell Script

我有一个 shell 脚本,我用它来访问 SMB 客户端:

#!/bin/bash
cd /home/username
smbclient //link/to/server$ password -W domain -U username
recurse
prompt
mput baclupfiles
exit

现在,脚本 运行s 访问服务器,然后要求手动输入命令。

谁能告诉我如何让 recursepromptmput baclupfilesexit 命令成为 运行 [=22] =] 脚本好吗?

我会采取不同的方法,将 autofs 与 smb 结合使用。然后您可以消除类似 smbclient/ftp 的方法并重构您的 shell 脚本以使用其他功能(如 rsync)来移动您的文件。这样您的凭据也不会存储在脚本本身中。您可以将它们埋在您的 fs 的某个地方,并使其只能由 root 读取,其他任何人都无法读取。

我想出了解决办法,分享给大家参考。

#!/bin/bash
cd /home/username
smbclient //link/to/server$ password -W domain -U username << SMBCLIENTCOMMANDS
recurse
prompt
mput backupfiles
exit
SMBCLIENTCOMMANDS

这会将两个 SMBCLIENTCOMMANDS 语句之间的命令输入 smb 终端。

smbclient 为此目的接受 -c 标志。

 -c|--command command string
       command string is a semicolon-separated list of commands to be executed instead of
       prompting from stdin.
       -N is implied by -c.

       This is particularly useful in scripts and for printing stdin to the server, e.g.
       -c 'print -'.

例如,您可能 运行

$ smbclient -N \\Remote\archive -c 'put /results/test-20170504.xz test-20170504.xz'

smbclient 执行完命令后断开连接。

smbclient //link/to/server$ password -W domain -U username -c "recurse;prompt;mput backupfiles"

我会评论 Calchas 的正确回答 approach-but 没有直接回答 OP question-but 我是新人,没有发表评论的名誉。

请注意,上面列出的 -c 是以分号分隔的命令列表(如其他答案中所述),因此添加递归和提示使 mput 无需提示即可复制。

您也可以考虑使用 -A 标志来使用文件(或解密文件以传递给 -A 的命令)来完全自动化此脚本

smbclient //link/to/server$ password -A ~/.smbcred -c "recurse;prompt;mput backupfiles"

其中文件格式为:

username = <username>
password = <password>
domain = <domain>
workgroup = <workgroup>

工作组和域都是可选的,但如果不使用 domain\username 格式的用户名,通常需要。

我怀疑这个 post 太晚了,无法满足这个特殊需求,但可能对其他搜索者有用,因为这个线程通过 -c 和分号引导我得到更优雅的答案。