SSHPASS
sshpass is the command used to authenticate the target machine. There are two ways of automating using ssh first using pass phrase key exchange between two machine second method is using sshpass providing password on to sshpass command line and running a command. In this post we will be looking into sshpass
Executing Command Line (Blocking)
# pass="*******"
# sshpass -p$pass ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no user1@hostname "ls -l"
# echo $?
Where pass is the password of the user named user1 present on target hostname and we can also check the return status of the command as well using echo $?
Executing Command Line (Non Blocking - Background)
# pass="*******"
# sshpass -p$pass ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no user1@hostname "bash -c 'nohup ./script.sh > /dev/null 2>&1 &'"
# echo $?
Upload the file using SCP (File transfer to target Machine)
# pass="*******"
# sshpass -p$pass scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no Linux.iso user1@hostname:/home/user1/.
# echo $?
Upload or Send or transfer the file Linux.iso from source machine on to target machine users home directory /home/user1
Download the file using SCP (File transfer to target Machine)
# sshpass -p$pass scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no user1@hostname:/home/user1/Linux.iso /root/extra/.
# echo $?
Download the file Linux.iso from target machine home directory /home/user1 to source machine directory /root/extra/
With the combination of above command we should be able to execute commands, transfer scripts and execute the commands in background and get the results that we required.
0 Comments