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

by Ramesh Natarajan on October 28, 2009

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
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

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

  1. 3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id
  2. How To Install, Edit, or Remove Cron Jobs in Batch Mode
  3. How To Backup Remote Linux Host Using rsnapshot rsync Utility
  4. Unix bc Command Line Calculator in Batch Mode
  5. Perform SSH and SCP Without Entering Password on openSSH
  

Vim 101 Hacks Book

Leave a Comment

Previous post:

Next post: