5 |
use Net::Telnet; |
use Net::Telnet; |
6 |
|
|
7 |
my $modem = '192.168.1.254'; |
my $modem = '192.168.1.254'; |
8 |
|
my @commands = ( |
9 |
|
':system config led=flash', |
10 |
|
':system config led=red', |
11 |
|
':system config led=off', |
12 |
|
'exit', |
13 |
|
); |
14 |
|
|
15 |
|
my $debug = 0; |
16 |
|
|
17 |
my $telnet = new Net::Telnet( $modem ) or die "Cannot telnet to $modem: $!\n"; |
my $telnet = new Net::Telnet( $modem ) or die "Cannot telnet to $modem: $!\n"; |
18 |
my $exp = Expect->exp_init($telnet); |
my $exp = Expect->exp_init($telnet); |
19 |
|
$exp->debug( $debug ); |
20 |
|
|
21 |
my ( $username, $password ) = ('Administrator',''); |
my ( $username, $password ) = ('Administrator',''); |
22 |
my $timeout = 15; |
my $timeout = 10; |
23 |
|
|
24 |
my $spawn_ok; |
my $spawn_ok; |
25 |
$exp->expect($timeout, |
$exp->expect($timeout, 'Username : '); |
26 |
[ |
$exp->send("$username\r\n"); |
27 |
qr'login: $', |
$exp->expect($timeout, 'Password :'); |
28 |
sub { |
$exp->send("$password\r\n"); |
29 |
$spawn_ok = 1; |
$exp->expect($timeout, '=>'); |
30 |
my $fh = shift; |
|
31 |
$fh->send("$username\n"); |
foreach my $cmd ( @commands ) { |
32 |
exp_continue; |
warn "sending >> $cmd\n"; |
33 |
} |
$exp->send( "$cmd\r\n" ); |
34 |
], |
$exp->expect($timeout, '=>'); |
35 |
[ |
} |
36 |
'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 |
|
|
); |
|