When you have the password-less login enabled, you may be either using SSH to execute command in the batch mode on a remote machine or using SCP to copy files from/to the remote machine.
If there are some issues with the password less login, your batch program may end up in a loop or timeout.
In this article, let us review how instruct ssh/scp to do the operation only if you can do without waiting for password.
Before you try this out, make sure password less login is setup between your local host and remote host.
1. ssh -o “BatchMode yes” Usage Example
If you have the password less login enabled, following example will login to the remote host and execute the who command without asking for the password.
local-host# ssh ramesh@remote-host who
If the password less login is not enabled, it will prompt for the password on the remote host as shown below.
local-host# ssh ramesh@remote-host who ramesh@remote-host's password:
If you use ssh -o “BatchMode yes”, then it will do ssh only if the password-less login is enabled, else it will return error and continues.
local-host# ssh -o "BatchMode yes" ramesh@remote-host Command
Batch mode command execution using SSH — success case
local-host# ssh -o "BatchMode yes" ramesh@remote-host who .. [Note: This will display the output of remote-host's who command]
Batch mode command execution using SSH — Failure case
local-host# ssh -o "BatchMode yes" ramesh@remote-host who Permission denied (publickey,password).
Note: If you didn’t use -o “BatchMode yes”, the above command would’ve asked for the password for my account on the remote host. This is the key difference in using the BatchMode yes option.
2. scp -B option Usage Example
If you use scp -B option, it will execute scp only if the passwordless login is enabled, else it will exit immediately without waiting for password.
$ scp -B file root@IP:PATH
SCP in Batch mode — Successful Case
local-host# scp -B /etc/yp.conf ramesh@remote-host:/tmp yp.conf 100% 84 0.1KB/s 00:00
SCP in Batch mode — Failure Case
In this example, if scp is possible without authentication, the command will execute else it will exit as shown below.
local-host# scp -B /etc/yp.conf ramesh@remote-host:/tmp Permission denied (publickey,password). lost connection