当我运行bash脚本,SSH的到远程服务器,并像wget运行一个命令它保存源未在目标服务器上?(Wh

2019-10-17 19:53发布

我有以下bash脚本

#!/bin/bash
ssh -o "StrictHostKeyChecking no" ubuntu@$1
cd /home/ubuntu
wget google.com

当我运行它, ./test.sh我SSH方式登录远程服务器,但wget命令不运行,除非我按ctrl + d,然后用SSH会话退出, index.html文件保存到/home/ubuntu

我怎样才能改变这种bash脚本,这样我可以保存index.html远程服务器上的文件吗?

Answer 1:

你的脚本执行:

  • 打开到指定的主机的交互连接
  • 等待连接完成(这就是为什么你必须按Ctrl-d)
  • 本地执行一些命令

你想要的是让远程主机上的一个脚本并执行它:

run.sh
#!/bin/sh
cd /home/ubuntu
wget google.com

而在本地主机上的脚本:

ssh -o "StrictHostKeyChecking no" ubuntu@$1 run.sh


Answer 2:

只需指定命令在命令行中执行ssh

ssh -o "StrictHostKeyChecking no" ubuntu@$1 'cd /home/ubuntu; wget google.com'


Answer 3:

你的bash非常错误的观念。 Bash是不是创建一个为你做打字宏的方式。 Bash是一个脚本语言,将在本地机器上执行一系列命令。 这意味着,它是将要运行的连接到远程主机的ssh命令。 一旦ssh的脚本执行完毕,bash将执行CD,然后wget的。

你想要做的是通过命令来远程SSH服务器。

 #!/bin/bash
 ssh -o "StrictHostKeyChecking no" ubuntu@$1 "cd /home/ubuntu; wget google.com"


文章来源: When I run a bash script that ssh's into a remote server and run a command like wget it saves on the source not the destination server?
标签: linux bash ssh