#!/usr/bin/perl use Net::DNS; use Getopt::Std; getopts("n"); $named=$opt_n; @ip=(); # get list of IP on interface open(IN,"ifconfig -a | grep \"inet addr\" |"); while () { chop; s/^\s+//g; s/\s+/ /g; $line=$_; @f=split(/ /,$line); @g=split(/:/,$f[1]); push(@ip,$g[1]); } close(IN); #for ($i=0;$i<@ip;$i++) #{ print ("IP=$ip[$i]\n"); } open(IN,"/proc/net/ip_conntrack"); while () { chop; s/\s+/ /g; $line=$_; @f=split(/ /,$line); $status=""; $source_ip=""; $source_port=""; $target_ip=""; $target_port=""; $target_name=""; $icmp_type=""; $icmp_code=""; $protocol=$f[0]; if ($protocol =~ /tcp/i) { $status=$f[3]; @g=split(/=/,$f[4]); $source_ip=$g[1]; @g=split(/=/,$f[5]); $target_ip=$g[1]; if ((!grep /$source_ip/, @ip) && (!grep /$target_ip/, @ip)) { @g=split(/=/,$f[6]); $source_port=$g[1]; @g=split(/=/,$f[7]); $target_port=$g[1]; if ($named==0) { $res = new Net::DNS::Resolver; $query = $res->query($target_ip,"PTR"); if ($query) { foreach $rr ($query->answer) { next unless $rr->type eq "PTR"; $target_name=$rr->ptrdname; last; } } } printf("%4s %15s/%5s -> %15s/%5s %s", "tcp",$source_ip,$source_port,$target_ip,$target_port,$status); if ($named==0) { printf(" (%s)",$target_name); } print ("\n"); } } elsif ($protocol =~ /udp/i) { @g=split(/=/,$f[3]); $source_ip=$g[1]; @g=split(/=/,$f[4]); $target_ip=$g[1]; if ((!grep /$source_ip/, @ip) && (!grep /$target_ip/, @ip)) { @g=split(/=/,$f[5]); $source_port=$g[1]; @g=split(/=/,$f[6]); $target_port=$g[1]; if ($named==0) { $res = new Net::DNS::Resolver; $query = $res->query($target_ip,"PTR"); if ($query) { foreach $rr ($query->answer) { next unless $rr->type eq "PTR"; $target_name=$rr->ptrdname; last; } } } printf("%4s %15s/%5s -> %15s/%5s %s", "udp",$source_ip,$source_port,$target_ip,$target_port,$status); if ($named==0) { printf(" (%s)",$target_name); } print ("\n"); } } elsif ($protocol =~ /icmp/i) { @g=split(/=/,$f[3]); $source_ip=$g[1]; @g=split(/=/,$f[4]); $target_ip=$g[1]; $icmp_type=$f[5]; $icmp_code=$f[6]; $status=$f[8]; if ((!grep /$source_ip/, @ip) && (!grep /$target_ip/, @ip)) { if ($named==0) { $res = new Net::DNS::Resolver; $query = $res->query($target_ip,"PTR"); if ($query) { foreach $rr ($query->answer) { next unless $rr->type eq "PTR"; $target_name=$rr->ptrdname; last; } } } printf("%4s %15s %5s -> %15s %5s %s", "icmp",$source_ip," ",$target_ip," ",$status); if ($named==0) { printf(" (%s)",$target_name); } print ("\n"); } } } close(IN);