How To: Use a SSH connection within a shell script
Original: May 23, 2008
Last Update: April 12, 2011
In an automated script I wanted to connect to a remote server and transfer files from the remote server to the local server. Not a problem with ftp or scp but how do you delete the remote files and folders? There are many solutions I guess, I used the one with an automated SSH connection to the remote server to delete the remote files.
The magic word is EOT. With the following solution you are able to let a script run and connect itself to a remote server and send remote commands. To not run into a problem with a password prompt, I suggest you exchange the keys of local and remote server.
echo "This script connects to a remote server and executes commands there."
ssh username@hostname <<EOT
rm -r *
If you receive this error:
Pseudo-terminal will not be allocated because stdin is not a terminal.
tcsetattr: stdin: Invalid argument
You need to modify the ssh command to the following:
ssh -t -t username@hostname <<EOT