Home > System Admin, Ubuntu > The closest thing to piping a password to SSH

The closest thing to piping a password to SSH

February 26th, 2008 admin Leave a comment Go to comments

There are a few cases in life where you want to connect to SSH without having to type in the password by hand. Most of the times you can use ssh-keygen and use private (or public) keys.

However, for the 0.5% of the time where you cannot use keys, you can use a little program called expect.

BTW, this does not, and should NEVER work:
echo 'mypassword' | ssh user@server

Installing Expect:
sudo apt-get install expect

Create the expect script:
nano connect.sh

Paste the basic code:

#!/usr/bin/expect
set timeout 60
spawn ssh -l username server
expect "password: $"
send "password\n"
expect "$ $"
interact

Replace ‘username’ and ’server’ on line 3 and ‘password’ on line 5.
The second to last line, ‘expect “$ $”‘ is for a bash shell. Replace the first ‘$’ with whatever your prompt is.

Save ( ctrl + o ) and Exit ( ctrl + x )
Make the script executable
chmod +x connect.sh

Credits

Categories: System Admin, Ubuntu Tags:
  1. No comments yet.
  1. No trackbacks yet.