1

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?

3 Answers 3

4

Just specify the command to be executed in the command line for ssh:

ssh -o "StrictHostKeyChecking no" ubuntu@$1 'cd /home/ubuntu; wget google.com'
Sign up to request clarification or add additional context in comments.

2 Comments

I like your succint way too, making a script can be overkill if you only need to run 2 commands.
@cnicutar It was exactly what I was about to comment on your answer but finally I resisted ;-)
3

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

Comments

2

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.