#Is_Host_Port_Open or Close
sub Is_pt_open{
for(my $tmp_port=$Start_pt;$tmp_port <= $End_pt;$tmp_port++){
my $mysock = IO::Socket::INET->new(PeerAddr => $_[0],
PeerPort => $tmp_port,
Proto => 'tcp') or next;
printf "The host %-15s Port %-8s is opening!\n",$_[0],$tmp_port;
$mysock->close;
}
}
#Is_ip_address_correct or Incorrect
sub Is_ip_correct{
foreach my $num(@_){
next if($num > 0 && $num < 255);
print "The Ip Address is not correct,Check your file!\n";
exit;
}
}
#Main Function
if(! defined $ARGV[0]){
&Usage();
exit 0;
}
open r_ip_addr,"$ARGV[0]" or die "open $ARGV[0] error!\n";
while(<r_ip_addr>){
chomp;
#Ip_Network_Range
if(&Is_ip_nk($_)){
foreach my $Ip_nt_addr(@ip_net){
#check a host for reachability
next if(!&Is_Host_Alive($Ip_nt_addr));
&Is_pt_open($Ip_nt_addr);
}
next;
}
#Single_IpAddress
if(!/(^\d+)\.(\d+)\.(\d+)\.(\d+)/){
&Usage();
exit 0;
}else{
&Is_ip_correct($1,$2,$3,$4);
my $Ip_addr = $_;
#check a host for reachability
next if(!&Is_Host_Alive($Ip_addr));
#check the status of a host's port:close or open
&Is_pt_open($Ip_addr);
}
}
