繁体   English   中英

当我运行一个ssh到远程服务器的bash脚本并运行类似wget的命令时,它将保存在源服务器上而不是目标服务器上吗?

[英]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?

我有以下bash脚本

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

当我运行它时,。/ ./test.sh我被ssh'到远程服务器中,但是除非我按ctrl + d ,否则wget命令不会运行,然后ssh会话退出并且index.html文件保存到/home/ubuntu

如何更改此bash脚本,以便可以将index.html文件保存在远程服务器上?

只需在ssh的命令行中指定要执行的命令即可:

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

您的脚本的作用是:

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

您想要在远程主机上创建一个脚本并执行它:

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

并在本地主机上的脚本中:

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

您对bash的概念非常错误。 Bash不是创建为您进行键入的宏的方法。 Bash是一种脚本语言,它将在本地计算机上执行一系列命令。 这意味着它将要运行一条连接到远程主机的ssh命令。 一旦ssh脚本执行完毕,bash将执行cd,然后执行wget。

您要做的是将命令传递到远程ssh服务器。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM