【问题标题】:Shell Script to open SSH connection execute a command用于打开 SSH 连接的 Shell 脚本执行命令
【发布时间】:2016-12-20 16:37:04
【问题描述】:

我很难创建一个脚本,该脚本获取包含子文件夹的文件夹,并打开与其他服务器的 ssh 连接,执行 axel 命令以从文件夹中下载每个文件,并在任务完成。

只要这个服务器和文件夹在互联网上打开,我就假装使用 Axel 来加速文件下载。

谁能帮帮我?

感谢

【问题讨论】:

  • 欢迎来到 Stack Overflow。请参阅How to AskHow to create a Minimal, Complete, and Verifiable example
  • 准确描述您遇到的问题。这不是“为您编写代码”服务;取而代之的是,我们正在尝试建立一个包含具有规范答案的紧密关注的问题的数据库;一个如此广泛的问题可以分解为许多较小的问题,而且很可能这些问题中的大部分问题已经在这里提出和回答了。

标签: bash shell ssh expect


【解决方案1】:

以下是使用 ssh 连接远程服务器的脚本

#!/usr/bin/expect -f

spawn ssh $username@$host bash script_to_run_on_server "$directory_path"
sleep 2
expect "password:" {
    send "${$password}\r"
}
interact

在上面的脚本中,host、username、password、directory_path 是你可以根据你的值替换的变量。

你对'execute an axel command to download each file from the folder, and erase the folder and the files when the job is done'的逻辑写在script_to_run_on_server中。

注意:请确保该目录已从服务器环境中读取、写入过量。

【讨论】:

  • 如果你必须鼓励人们在程序化使用过程中传递明文密码,为什么不建议sshpass,而不是用expect重新发明轮子?
  • ...也就是说,我在问题中看不到任何需要明文密码(例如 RSA 密钥)的内容。
  • @CharlesDuffy- 我不知道 sshpass。我将浏览它的手册页
猜你喜欢
  • 2018-03-29
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 2018-10-06
  • 2019-10-26
  • 2015-12-26
相关资源
最近更新 更多