use strict; use warnings; use Expect; use Term::ReadKey; my $exp = new Expect; my $usr = 'myuser'; my $host = 'myhost'; my $pubkey = `cat $ENV{HOME}/.ssh/id_rsa.pub`; if(!$pubkey) { die "No public key" }; $exp->spawn("ssh $usr\@$host 'echo \"$pubkey\" >>.ssh/authorized_keys'") || die "Dead Spawn"; $exp->expect(15, # This handles a first-time query from ssh about adding the target # machine to your known_hosts file if it isn't already there... [ '-re', '.*\(yes/no\)\? ', sub{ $exp->send("yes\n"); $exp->exp_continue; } ], # This handles the password prompt... [ 'assword: ', sub{ $exp->send(_GetPass($host, $usr) . "\r"); $exp->exp_continue; } ], # Handling EOF... [eof => sub { print "\nERROR: Got an EOF...\n"; } ], # Handling timeouts... [timeout => sub { die "\nERROR: Got a Timeout..\n"; } ], ); # This normally would be a GUI popup... sub _GetPass { my $host = shift || die "Missing host"; my $usr = shift || die "Missing usr"; ReadMode('noecho'); print "Enter $usr" . '@' . "$host Password: "; chomp(my $pw = ReadLine(0)); ReadMode('restore'); print "\n"; return ( $pw ); }