简体   繁体   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?

I have the following bash script 我有以下bash脚本

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

When I run it, ./test.sh I am ssh'd into the remote server but the wget command is not run unless I press ctrl+d and then the ssh session exits and the index.html file is saved to /home/ubuntu 当我运行它时,。/ ./test.sh我被ssh'到远程服务器中,但是除非我按ctrl + d ,否则wget命令不会运行,然后ssh会话退出并且index.html文件保存到/home/ubuntu

How can I change this bash script so that I can save the index.html file on the remote server? 如何更改此bash脚本,以便可以将index.html文件保存在远程服务器上?

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

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

What your script does: 您的脚本的作用是:

  • Open an interactive connection to the specified host 打开与指定主机的交互式连接
  • Wait for the connection to finish (that's why you have to hit Ctrl-D) 等待连接完成(这就是为什么必须按Ctrl-D的原因)
  • Execute some commands locally 在本地执行一些命令

What you want is to make a script on the remote host and execute it: 您想要在远程主机上创建一个脚本并执行它:

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

And in the script on the local host: 并在本地主机上的脚本中:

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

You have the concept of bash very wrong. 您对bash的概念非常错误。 Bash is not a way to create macros that do the typing for you. Bash不是创建为您进行键入的宏的方法。 Bash is a scripting language that will execute a series of commands on the local machine. Bash是一种脚本语言,它将在本地计算机上执行一系列命令。 This means that it is going to run an ssh command which connects to the remote host. 这意味着它将要运行一条连接到远程主机的ssh命令。 Once the ssh script is done executing, bash will execute the cd and then the wget. 一旦ssh脚本执行完毕,bash将执行cd,然后执行wget。

What you want to do is pass the commands to the remote ssh server. 您要做的是将命令传递到远程ssh服务器。

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

暂无
暂无

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

相关问题 切换用户时,用于运行远程脚本的ssh命令在远程服务器上存在shell - ssh command to run remote script exist shell on remote server when switching user 如何使用perl脚本和ssh在远程linux服务器中运行bash或shell脚本 - How to run a bash or shell script in a remote linux server using perl script and ssh 如何进入服务器并运行需要bash变量和sshes到另一台服务器的bash脚本? - How do I ssh into a server and run a bash script that requires bash variables and sshes into another server? 在远程服务器上运行命令 - Run a command in remote server 使用带有ssh的参数运行远程bash脚本 - run a remote bash script with arguments with ssh 通过代理服务器SSH到远程计算机以运行带有参数的脚本 - ssh to remote machine via proxy server to run a script with parameters 如何在 200 台服务器上运行脚本并在堡垒实例终端中打印输出? 我的 bash 脚本挂在远程服务器的 ssh 上 - How to run script on 200 servers and print output in bastion instance terminal? My bash script hangs on ssh to remote server 如何将变量替换为bash脚本,然后在Linux中的远程服务器上运行 - How to substitute variables into bash script and then run on remote server in Linux 如何在Windows Task Scheduler中运行bash脚本(位于远程服务器上)? - How do I run a bash script (that resides on a remote server) in windows task scheduler? 通过SSH在远程计算机上运行bash脚本时,如何在本地计算机上触发声音? - How do I get a sound to fire on local machine when I run a bash script on a remote computer via SSH?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM