分类:
2009-10-30 14:54:32
#!/usr/bin/perl -w use strict; use Expect; my $exp = new Expect; #$exp->log_stdout(1); #$exp->exp_internal(1); #$exp->restart_timeout_upon_receive(1); #$exp->debug(1); #$exp->raw_pty(1); system("stty -echo"); my $passwd= system("stty echo"); $exp->slave->clone_winsize_from(\*STDIN); $exp->spawn("ssh lieyuan\@console223"); #$SIG{WINCH} = \&winch; $exp->expect(10, [ qr/Password:\s*$/ => sub { exp->send("$passwd\n");exp_continue; } ], [ qr/\$\s*$/ => sub { $exp->send("ssh root\@console221\n");} ], ); $exp->interact(); $exp->soft_close(); $exp->hard_close(); sub winch { $exp->slave->clone_winsize_from(\*STDIN); kill WINCH => $exp->pid if $exp->pid; $SIG{WINCH} = \&winch; } ~ |
#!/usr/bin/env perl -w use strict ; use Fcntl; my $flag = 0; $SIG{'ALRM'} = sub { $flag = 1; print "$_ timeout\n"; }; open LOG, ">/tmp/log" or die; while ( ) { chomp; alarm( 60 ); my $pid = open( my $file, "-|", "/usr/bin/nmap -sP $_"); noblocking( $file ); print LOG "--------------------------------\n"; print LOG "$_\n"; while ( 1 ) { sleep 1; my $line = <$file>; if ( defined( $line ) and $line =~ m/is up/ ) { print LOG "\t$line\n"; alarm( 0 ); kill 9, $pid; close $file; last; } if ( $flag == 1 ) { $flag = 0; kill 9, $pid; close $file; last; } } } sub noblocking { my $handle = shift; my $fl = fcntl ( $handle, F_GETFL, 0 ) or die ; $fl |= O_NONBLOCK; fcntl ( $handle, F_SETFL, $fl ) or die ; } __DATA__ 118.67.112.0/20 118.63.112.0/20 |
#!/usr/bin/env perl -w #Author lieyuan@taobao.com #Date 2009/11/04 use strict ; use Fcntl; my $flag = 0; open LOG, ">/tmp/log" or die; while ( ) { chomp; my $pid = open( my $file, "-|", "/usr/bin/nmap -sP $_") or die; print LOG "--------------------------------\n"; print LOG "$_\n"; eval { local $SIG{'ALRM'} = sub { die "timeout\n"; }; alarm( 6 ); while ( my $line = <$file> ) { alarm( 0 ); if ( defined( $line ) and $line =~ m/is up/ ) { print LOG "\t$line\n"; kill 9, $pid; close $file; last; } alarm( 6 ); } }; if ( defined ($@) ) { print LOG "\ttimeout\n"; kill 9, $pid; close $file; } } __DATA__ 118.67.112.0/20 118.63.112.0/20 117.63.112.0/20 |