I'm trying to SSH to a server on startup with a .sh script, but that will require me to enter the password for the account on the server that I'm SSHing to. I did some RTFMing, and I see in "-o" that it has "PasswordAuthentication" but I'm not sure how or if I could use that option. As this will be in a shell script, obviously I'd like to have the password in that file, or in any case not have to enter in the password manually every time the script runs.
Asked
Active
Viewed 9,443 times
3 Answers
2
I'm on my phone now so I can't give you a detailed answer. The proper solution here in my opinion is to use key based authentication. Basically you use ssh-keygen (I think) to generate a public/private key pair. Since you need script access, enter a blank password when prompted. Then, follow the relevant man pages to install the public key on the server and install the private key in its proper place.
Now you should be able to log in to that server securely and without a password. Note that by securely I mean as long as your user account isn't compromised.

Scott Severance
- 14,056
0
This thread on ServerFault suggests two ways, one with sshpass
:
sudo apt-get install sshpass
sshpass -p your_password ssh user@hostname
and one with expect
:
#!/usr/bin/expect -f
# ./ssh.exp password 192.168.1.11 id
set pass [lrange $argv 0 0]
set server [lrange $argv 1 1]
set name [lrange $argv 2 2]
spawn ssh $name@$server
match_max 100000
expect "*?assword:*"
send -- "$pass\r"
send -- "\r"
interact

miguelmorin
- 291
Bad port 'umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys'
– Eva Dec 01 '12 at 09:55