看了一些前辈的多线程文章,感觉理解了差不多了。就写了这个小程序。
这个程序有点乱,不过没有时间整理写注释,回头有空写成mode吧。
#!/usr/bin/perl use threads; use Net::Ping; $p=Net::Ping->new(); sub slep_ping{ local($host_ip)=shift; if($p->ping($host_ip)) { print "$host_ip is access---------OK!\n"; }else{ print "$host_ip is No access\n"; } $p->close(); } sub ip_count{ while($ip_start != $ip_end){ push @ip_group,("$ip."."$ip_start"); $ip_start++; } return @ip_group; } system('clear'); print "please set the IP(C):"; chomp($ip=<STDIN>); print "StartIP:"; chomp($ip_start=<STDIN>); print "EndIP:"; chomp($ip_end=<STDIN>); my $thread_num=0; &ip_count; foreach $key (@ip_group){ if($thread_num >= 200){ for my $t (threads->list(threads::joinable)){ $t->join(); $thread_num--; } redo; } threads->create(\&slep_ping,$key); $thread_num++; } for my $t(threads->list()){ $t->join; }