my $exp = Expect->spawn("telnet 192.168.1.150") or die "Cannot spawn telnet: $!\n";; my ($username,$password,$timeout); $username = 'ackerman'; $password = 1454; $timeout = 1; print localtime(time)."\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 expect ); #### [root@localhost Perl_Ping]# perl telnet.pl Mon Aug 26 11:48:21 2013 Trying 192.168.1.150... Connected to 192.168.1.150. Escape character is '^]'. User name: [root@localhost Perl_Ping]#