How to automate SSH login with password? I'm configuring my test VM, so heavy security is not considered. SSH chosen for acceptable security with minimal configuration.

ex)

echo password | ssh id@server

This is not working.

I remember I did this with some tricks somebody guided me, but I can't remember now the trick I used...

link|improve this question

70% accept rate
FreeBSD did not accept password-less keys. Don't be tempted. However some Linux servers accepted it. I believe the Linux server was misconfigured. – Eonil Mar 1 '11 at 14:32
This is a valid question. For example, I want to allow a user to enter a password, then login in to another machine using it. I can't assume that there will be ssh keys distributed across all our machines. The answers below so far do not help this situation. – dfrankow Apr 12 at 16:30
feedback

6 Answers

up vote 8 down vote accepted

Don't use a password. Generate a passphraseless ssh key and push it to your VM.

If you already have an ssh key, you can skip this step... Just hit enter for the key and both passphrases:

$ ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/id_rsa.pub.

Copy your keys to the target server:

$ ssh-copy-id id@server
id@server's password: 
Now try logging into the machine, with "ssh 'id@server'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

Finally check logging in...

$ ssh id@server

id@server:~$ 

You may also want to look into using ssh-agent if you want to try keeping your keys protected with a passphrase.

link|improve this answer
I finally decided using key pairs. Because I realized that's the most simple way. – Eonil Mar 1 '11 at 12:49
@Eonil: Don't be tempted to use keys without a pass phrase. Learn how to use ssh-agent or pageant. – Iain Mar 1 '11 at 13:20
@Iain Wow. I tempted, and refused already :) Linux server accepted the password-less keys but FreeBSD did not. I'm connecting from Mac, and I believe the password input GUI does same thing ssh-agent does. Because it does not ask anymore they once asked. – Eonil Mar 1 '11 at 14:30
feedback

Sure you don't want to use SSH keys rather than passwords? That way it's both secure and automatic.

link|improve this answer
I realized using key-pairs is most simple way. Thanks for care :) – Eonil Mar 1 '11 at 12:50
feedback

Hi I hope this guide helps you:

http://jcsalterego.github.com/2011/02/04/getting-comfortable-with-ssh.html

It goes over keyless entry and aliases which might be helpful to you.

link|improve this answer
This guide helped me partially. Thanks :) – Eonil Mar 1 '11 at 12:51
feedback

why so complicated?

Generate a rsa keypair:

# ssh-keygen

then copy it on the server with one simple command:

# ssh-copy-id hostname

you can now log in without password:

# ssh hostname
link|improve this answer
feedback

This might not be any use to you, but you can do it with Perl:

#!/usr/bin/perl
use warnings;
use strict;

use Net::SSH::Perl;
my $host = 'remote.serv.er';
my $user = 'root';
my $pass = 'hunter2';
my $ssh = Net::SSH::Perl->new('$host');
$ssh->login('$user', '$pass') or die "Oh noes! $!";

link|improve this answer
@JLo - have a +1 for good measure (and to cancel out the -1!). – Coops Mar 1 '11 at 14:32
Thanks Coops :) I did think it was a bit harsh! I'm keeping an eye on your reputation, I see you're sneaking back up on me. – James Lawrie Mar 1 '11 at 14:42
feedback

Use 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

Example:

# ./1.ex password localhost ooshro
spawn ssh ooshro@localhost
ooshro@localhost's password: 
Linux ubuntu-1010-server-01 2.6.35-25-generic-pae #44-Ubuntu SMP Fri Jan 21 19:01:46 UTC 2011 i686 GNU/Linux
Ubuntu 10.10

Welcome to Ubuntu!
 * Documentation:  https://help.ubuntu.com/
Last login: Tue Mar  1 12:41:12 2011 from localhost
link|improve this answer
It worked but it can't print stdout of remote machine. – Eonil Mar 1 '11 at 12:41
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.