Tuesday 3 March 2015

sshpass : SSH login without waiting for password prompt

Sometimes, while writing scripts, we may come across a requirement that, SSH should not wait for password prompt.

This can be done very easily with sshpass.

Download and install sshpass as below:
----
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/sshpass-1.05-1.el6.x86_64.rpm
rpm -ivh sshpass-1.05-1.el6.x86_64.rpm
-----

At this point, sshpass command would have been installed in your system.

Now, lets have a scenario, I have few servers all of them have same password "atom". I have to find out the number of processors in each system. I am lazy to login to each system and type the command "nproc" to get this done.

So, I developed a script:
----
#!/bin/bash

for i in `cat /var/tmp/ips`
do
{
sshpass -p 'atom' ssh root@$i "
nproc"
}
done
------

NOTE: THE LINE MARKED IN RED IS THE METHOD TO PASS THE PASSWORD 'atom'.

The "/var/tmp/ips" contains ips of all servers.  Now, execute the script this will give the output as the number of cpus for all servers specified in "ips" file.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.