sshpass Automation SCP and running command in Background

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
 
sshpass scp ssh

 

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 $?
 
Where pass is the password of the user named user1 present on target hostname and bash is the parent process executing the command nohup and output will be redirected to /dev/null (If needed we can redirect the output to any specif file). Here the command will run in background and cannot get the exit status of the command line executed on remote machine.
 

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)

# pass="*******"
# 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.


Post a Comment

0 Comments