0

I am trying to create a bash script that will scp a script to a remote server, ssh (using an ssh key that is already installed) to the remote server, execute the uploaded script, and then delete the remote script when it is finished. I'm not clear how to run an ssh session inside a bash script. Here are the commands I use to do it from the command line:

scp my_script.sh [email protected]:/usr/home/user/
ssh [email protected]
>sh my_script.sh
>rm myscript.sh
>exit

How do I script the ssh portion of my command list? Thanks!

3 Answers 3

3

ssh will execute commands passed as arguments on the remote machine. The following should do what you want:

ssh [email protected] 'sh my_script.sh; rm my_script.sh'
1
  • I'd use && instead of ; if I wanted the script to not be deleted on failure. Jun 17, 2010 at 23:19
0

You could also skip the file copy and delete and do something like this.

cat my_script.sh | ssh user@host bash
0

You could also investigate the use of Fabric, a Python library for systems admin over SSH:

http://docs.fabfile.org/0.9.1/

It's super-simple to get started with but very powerful, especially across multiple servers.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .