简体   繁体   中英

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

#!/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

How can I change this bash script so that I can save the index.html file on the remote server?

只需在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)
  • 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 is not a way to create macros that do the typing for you. Bash is a scripting language that will execute a series of commands on the local machine. This means that it is going to run an ssh command which connects to the remote host. Once the ssh script is done executing, bash will execute the cd and then the wget.

What you want to do is pass the commands to the remote ssh server.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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