|
From: <RGi...@a1...> - 2002-04-18 09:57:09
|
> I have a stupid question:
>
> I want to do the following but in perl using the expect perl
> module.
> ----------- Start expect code
> spawn ssh -l $user $argv
> expect "${user}@${argv}'s password:"
> send "$password\r"
From the Expect manpage:
"I want to automate password entry for su/ssh/scp/rsh/...
You shouldn't use Expect for this. Putting passwords, especially
root passwords, into scripts in clear text can mean severe security
problems. I strongly recommend using other means. For 'su', consider
switching to 'sudo', which gives you root access on a per-command and
per-user basis without the need to enter passwords. 'ssh'/'scp' can be
set up with RSA authentication without passwords. 'rsh' can use
the .rhost mechanism, but I'd strongly suggest to switch to 'ssh'; to
mention 'rsh' and 'security' in the same sentence makes an oxymoron.
It will work for 'telnet', though, and there are valid uses for it,
but you still might want to consider using 'ssh', as keeping cleartext
passwords around is very insecure.
Source Examples
How to automate login
my $exp = Expect->spawn("telnet localhost")
or die "Cannot spawn telnet: $!\n";;
my $spawn_ok;
$exp->expect($timeout,
[
qr'login: $',
sub {
$spawn_ok = 1;
my $fh = shift;
$fh->send("$username\n");
exp_continue;
}
],
[
'Password: $',
sub {
my $fh = shift;
print $fh "$password\n";
exp_continue;
}
],
[
eof =>
sub {
if ($spawn_ok) {
die "ERROR: premature EOF in login.\n";
} else {
die "ERROR: could not spawn telnet.\n";
}
}
],
[
timeout =>
sub {
die "No login.\n";
}
],
'-re', qr'[#>:] $', #' wait for shell prompt, then exit
);
Hope this helps,
Roland
--
RGi...@cp...
|