Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using expect to pass a password to ssh

Tags:

ssh

cygwin

expect

How can I use expect to send a password to an ssh connection.

say the password was p@ssword and the ssh command was ssh [email protected]

What would I do with expect to a make it input the password when it says

[email protected]'s password:
?

The proper action of using an SSH key pair isn't an option because I would have to use ssh (scp) to put the key on the server, which would ask for a password.

like image 612
Malfist Avatar asked Jan 19 '09 21:01

Malfist


People also ask

Can we pass password in ssh command?

sshpass command-line tool will do the job for us. It facilitates a simplified approach to non-interactive ssh sign-in and supports one-liner ssh password input. Firstly, you need to install the sshpass tool on your Linux operating system.

How do I put password in ssh?

First, open a terminal and run ssh-copy-id yourservername . You'll be asked to enter your password for the server. After entering your password, you'll be able to SSH into the server without needing a password again.


3 Answers

I always used the "proper" solution, but I used expect in other situations.

Here I found following suggestion:

#!/usr/local/bin/expect
spawn  sftp  -b cmdFile [email protected]
expect "password:"
send "shhh!\n";
interact
like image 93
max Avatar answered Oct 08 '22 08:10

max


Would it not be easier to use public key authentication and use a key with no passphrase?

As the user on the source machine do this to make an RSA key

ssh-keygen -t rsa

Now copy ~/.ssh/id_rsa.pub to the target machine and append it to the authorized_keys file of the target user

like image 4
Paul Dixon Avatar answered Oct 08 '22 08:10

Paul Dixon


Your quickest way forward (unless you want to become a Tcl expert, which would be... unusual... in 2009) is probably to use autoexpect. Here's the man page:

http://expect.nist.gov/example/autoexpect.man.html

In short, fire up autoexpect, run your ssh session, finish up what you need to do, stop autoexpecting and then beat your keyboard over the resulting mess until it works :) I'm assuming you don't need anything more than a quick hack to get your keys sorted out and then, well it sounds like you know the score already with that.

And there's this question which already contains an example close to what you seek.

like image 4
Martin Carpenter Avatar answered Oct 08 '22 06:10

Martin Carpenter