2

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.

7 Answers 7

2

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"

8

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>
Eg
$ ssh-copy-id [email protected]

1
  • 2
    Huh. I didn't know about the ssh-copy-id utility. That makes life much nicer. Feb 19, 2011 at 15:10
6

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.

2
  • hi, thank for reply. But do you know expect shell scrip, b'coz I have multiple scenario for giving password. Feb 19, 2011 at 14:59
  • What multiple scenario? Please explain. Feb 19, 2011 at 15:00
2

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...

1
  • ...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, 2011 at 21:22
1

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.

1

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

ssh-keygen 
scp .ssh/id_rsa.pub user@server:.
ssh user@server "cat id_rsa.pub >> .ssh/authorized_keys"
ssh user@server "rm ~/id_rsa.pub"
0

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:

 /home/user/.ssh/authorized_keys2

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

You must log in to answer this question.

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