≡ Menu

How To Execute SSH and SCP in Batch Mode (Only when Passwordless login is enabled)

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
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • tough July 16, 2012, 2:08 pm

    Its a good article for start up but now after this I am having different problem. I have successfully tested password less log for ssh but not able to scp to transfer the file, I have been getting connection time out error for long and have not been able to resolve. Some light on this scenario would be appreciated.

  • QNeill August 22, 2013, 9:00 am

    I prefer to format my ssh options without quotes and spaces; then they avoid bash (and ssh) quoting problems:

    ssh -oBatchmode=yes …