Chinaunix首页 | 论坛 | 博客
  • 博客访问: 477650
  • 博文数量: 104
  • 博客积分: 3455
  • 博客等级: 中校
  • 技术积分: 1216
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-18 18:33
文章分类

全部博文(104)

文章存档

2015年(3)

2014年(1)

2013年(7)

2012年(8)

2011年(11)

2010年(18)

2009年(56)

我的朋友

分类:

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
阅读(1210) | 评论(0) | 转发(0) |
0

上一篇:加班

下一篇:伪终端

给主人留下些什么吧!~~