Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a shell script that ssh'es into a server using public key authentication and runs a command on that machine. This works fine on machines where the users have installed the public key on the server. If this script is run from a machine where the user hasn't set up the authentication key, then the script will be stuck at password prompt. I want my script to carry on instead of getting stuck at the prompt. How can I bypass the prompt? I'm on a Mac OSX. The server is also running OSX

share|improve this question

One way to do this is by specifying -o PasswordAuthentication=No as part of your SSH command:

ssh -o PasswordAuthentication=no user@server

This removes PasswordAuthentication from the list of authentication choices that the SSH client has to talk to the server, prevent you from sticking on the Password prompt. Note that this also allows other authentication mechanisms other than PubkeyAuthentication to work (RHostsRSAAuthentication, etc). If you want to use PubkeyAuthentication only...

ssh -o PreferredAuthentications=publickey user@server

share|improve this answer
Nice answer ;-) – Tim May 30 '12 at 20:56

You can write a shell script in which you can provide the password which will do automatic login into the ssh server.

But you should have expect package installed for this script.

In Redhat, expect package comes by default. But in Ubuntu you have to install it separately.

You can check this by using commands:

$ rpm -qa | grep expect for Redhat

$ dpkg -l expect for Ubuntu

You can write a shell script which will provide the ssh server's password as follows:

spawn ssh user@ssh-server-ip   
expect "user@ssh-server-ip's password:"    
send "password\r"       

This script should do your task.

share|improve this answer
Storing (user) passwords in clear text in a file is bad practise. Using an Expect script for interacting with SSH is unreliable, it may break over version upgrades. – jippie May 30 '12 at 7:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.