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\n" 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\n", $port, $srv_name || "unknown serv");
close(S);
}
}
exit 0;
[/code:1:d7df680c80]