BASH – Automating SCP/SSH/RSYNC connections with EXPECT

Published on Author gryzliLeave a comment

Very often I need to automate some task which requires user input, most of the times this input is some type of credentials (user + password) or sometime some more.

I have find that expect is very useful in such cases.

 

Before trying any of the scripts below, make sure you have expect installed on your system. Most of the time installing expect is as simple as doing “zypper/yum/apt-get install expect”.

 

Here are some simple expect scripts which can be used to automate SSH/rsync/scp  connections by providing them with credentials.

 

Executing batch SSH commands with password authentication using expect

First create the expect script file called: ssh.exp 

 

Second, set some execute permissions on the file:

Finally you can run the script with the following usage:

Here some example with values, which will execute “uptime” with root user on a host 192.168.1.1

 

Executing batch rsync with password authentication using expect

First create the expect script file called: rsync.exp 

 

Second, set some execute permissions on the file:

Finally you can run the script with the following usage:

– This will copy current /root/work/ content to the destination one

or you could try the oposite:

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha * Time limit is exhausted. Please reload CAPTCHA.