Super User is a question and answer site for computer enthusiasts and power users. 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

can anybody tell me how can I create a shell script for ssh where I don't have to type the password manually?

I have learned expect command and I have tried multiple examples, but it didn't work. Can anyone help me?

Please reply me as soon as possible.

share|improve this question

migrated from Feb 19 '11 at 21:10

This question came from our site for professional and enthusiast programmers.

up vote 2 down vote accepted

If for whatever reason you need to rely only on expect, try something like (all one line):

expect -c "spawn ssh <username>@a<address> <ssh commands/options> ; expect assword ; send $PASSWORD\n ; interact"

share|improve this answer

You could use ssh-keygen and ssh-copy-id to create a rsa key and add you key as allowed host for the remote computer.

Create a ssh key:
$ ssh-keygen -t rsa

Copy it to the remote computer as a allowed host:
$ ssh-copy-id user@<IP/hostname>
$ ssh-copy-id username@12.345.67.890

share|improve this answer
Huh. I didn't know about the ssh-copy-id utility. That makes life much nicer. – JUST MY correct OPINION Feb 19 '11 at 15:10
Wow. This is great. – chrisjlee Jun 14 '11 at 16:48

You can use the ssh agent for providing the password only once during entire session, or use key authentication with no password.

Personally, I highly recommend you to not use such scripts where you either specify passwords on the command line, or by storing them in a text file. If your system will be compromised, you will definitely regret about that. Take in mind, that the first thing bad people looking into is your .bash_history.

share|improve this answer
hi, thank for reply. But do you know expect shell scrip, b'coz I have multiple scenario for giving password. – Lokesh Paunikar Feb 19 '11 at 14:59
What multiple scenario? Please explain. – Andrejs Cainikovs Feb 19 '11 at 15:00

Please consider not using a password at all and instead use a asymmetric key (e.g. RSA). It provides much better security and removes all the hassle of maintaining passwords...

share|improve this answer
...and password protect your key as an extra layer of security. If you accidentally loose the media with your unprotected key on it, you're in danger. – Lekensteyn Feb 19 '11 at 21:22

Don't put your password into a shell script. Shell scripts are not the place to put your password. Putting your password into a shell script is inadvisable. It is not recommended to put your password into a shell script.

If the above advice is not clear, repeat it a few dozen more times until it is.

With SSH your best bet is to use ssh-keygen to make a public/private key pair. Then, on the remote machine, in ~/.ssh (or wherever your SSH has been set up to look for its configuration) edit the file authorized_keys and paste your PUBLIC key. The file will, afterwards, look something like this:

ssh-rsa AAAAB3NzaC1yc2EAAAABJQ...elided for security...fdVEkAPINVd0=rsa-key-20100920

When that's in place you won't need a password to sign in.

edited to add

You need to place your PRIVATE key file into your local ~/.ssh directory and make sure that only you can read it.

further edited to add

This answer mentions the ssh-copy-id utility which replaces all the manual editing I mentioned. Nice touch that. Use that instead.

share|improve this answer

ssh-key:s is used for that, and basic usecase to add one looks like this:

scp .ssh/ user@server:.
ssh user@server "cat >> .ssh/authorized_keys"
ssh user@server "rm ~/"
share|improve this answer

You need to create a private/public key pair.

Then, write down your public key to the remote host you are going to ssh to this file:


There are a lot of tutorials on how to do that. Just GOOGLE for: "ssh without password authorized_keys2".

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .