perl script for scan TCP ports .(轉)

post0發表於2007-08-10
perl script for scan TCP ports .(轉)[@more@]

A simple perl script that scans a machine for reachable TCP ports.

[code:1:d7df680c80]

#!/usr/bin/perl

$|=1;

$tghost = shift || $ENV{"HOSTNAME"};

$maxprt = shift || 1024;

$AF_INET=2;

$SOCK_STREAM=1;

$sockaddr='S n a4 x8';

($name,$aliases,$proto)=getprotobyname('tcp');

foreach $port (1 .. $maxprt)

{

($name,$aliases,$type,$len,$thataddr)=gethostbyname($tghost);

$this=pack($sockaddr,$AF_INET,0,$thisaddr);

$that=pack($sockaddr,$AF_INET,$port,$thataddr);

die "unknown host $tghost " if($thataddr eq "");

socket(S,$AF_INET,$SOCK_STREAM,$proto) or die $!;

bind(S,$this) or die $!;

if(connect(S,$that))

{

($srv_name, $srv_aliases, undef, undef)=getservbyport($port,'tcp');

printf("%5d %s ", $port, $srv_name || "unknown serv");

close(S);

}

}

exit 0;

[/code:1:d7df680c80

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-943003/,如需轉載,請註明出處,否則將追究法律責任。

相關文章