Header RSS Feed
» IT tipps and howto's

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
cd /home/someuser
ls -l
rm -r *
exit 0

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

Source: http://www.unix.com/unix-advanced-expert-users/26124-pseudo-terminal-will-not-allocated-because-stdin-not-terminal.html

Go to Homepage home
Linux Howtos how to's
Monitoring Plugins monitoring plugins
Links links

Valid HTML 4.01 Transitional
Valid CSS!
[Valid RSS]

6979 Days
until Death of Computers