Totally stolen via some internet page. ssh and scp use the same -o "BatchMode". The scp -B option is also very handy, however scripts are already running and I'm not going to change them.
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