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 |
|
); |
11 |
|
|
12 |
|
while(<>) { |
13 |
|
chomp; |
14 |
|
next if (/^#/ || /^\s*$/); |
15 |
|
push @commands, $_; |
16 |
|
} |
17 |
|
|
18 |
|
push @commands, ':system config led=off'; |
19 |
|
|
20 |
|
my $debug = 0; |
21 |
|
|
22 |
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"; |
23 |
my $exp = Expect->exp_init($telnet); |
my $exp = Expect->exp_init($telnet); |
24 |
|
$exp->debug( $debug ); |
25 |
|
|
26 |
|
$exp->log_stdout( 1 ); |
27 |
|
|
28 |
my ( $username, $password ) = ('Administrator',''); |
my ( $username, $password ) = ('Administrator',''); |
29 |
my $timeout = 15; |
my $timeout = 3; |
30 |
|
|
31 |
|
$exp->expect($timeout, 'Username : '); |
32 |
|
$exp->send("$username\r\n"); |
33 |
|
$exp->expect($timeout, 'Password :'); |
34 |
|
$exp->send("$password\r\n"); |
35 |
|
$exp->expect($timeout, '=>'); |
36 |
|
|
37 |
|
foreach my $cmd ( @commands ) { |
38 |
|
$exp->send( "$cmd\r\n" ); |
39 |
|
$exp->expect($timeout, '=>'); |
40 |
|
} |
41 |
|
|
42 |
|
$exp->send( "exit\r\n" ); |
43 |
|
$exp->soft_close(); |
44 |
|
|
|
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 |
|
|
); |
|