Header
 
» IT tipps and howto's

How To: Use a SSH connection within a shell script

Introduction
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.

Solution
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.

#!/bin/bash
echo "This script connects to a remote server and executes commands there."
ssh username@hostname <<EOT
cd /home/someuser
ls -l
rm -r *
exit
EOT
exit 0

Last Update: 23.05.2008


Go to Homepage home RSS Feed
About ck about
Linux Howtos how to's

 

 

Valid HTML 4.01 Transitional
Valid CSS!
[Valid RSS]

10036 Days
until Death of Computers