f800632f7be1aa031506d9fcf2a6df3fb928e9cb
[vsys-scripts.git] / root-context / exec / portsummary
1 #!/usr/bin/python
2
3 import os
4 import sys
5
6 # NOTE: '--inet' lists only ipv4 addresses.
7 ns = os.popen("ncontext --nid 1 --migrate -- vcontext --xid 1 --migrate -- netstat -apnlut --inet", 'r')
8 port_summary = {}
9 for line in ns:
10         try:
11                 ns_fields = line.split()
12                 if ns_fields[0] == "tcp" or ns_fields[0] == "udp":
13                         (src_ip, src_port) = ns_fields[3].split(':')
14                         (dst_ip, dst_port) = ns_fields[4].split(':')
15
16                         port_key='%s-%s' % (src_port, dst_port)
17
18                         if src_ip is not "0.0.0.0" and port_key in port_summary:
19                                 # skip INADDR_ANY addresses and ports we've already seen.
20                                 continue
21
22                         conn_state = ns_fields[5]
23                         if ns_fields[0] == "tcp":
24                                 proc_field = ns_fields[6]
25                         else:
26                                 if conn_state == "ESTABLISHED":
27                                         proc_field = ns_fields[6]
28                                 else:
29                                         proc_field = ns_fields[5]
30
31                         if proc_field != "-":
32                                 (pid,procname)= proc_field.split('/')
33                         else:
34                                 # NOTE: without a PID there is no way to associate with an XID
35                                 continue
36
37                         if ( ns_fields[0] == "tcp" and src_ip == "0.0.0.0" and conn_state == "LISTEN" ) or \
38                            ( ns_fields[0] == "udp" and src_ip == "0.0.0.0" ):
39                                 type='C'
40                         elif src_ip == "127.0.0.1":
41                                 type='l'
42                         elif src_ip != "0.0.0.0" and src_ip != "127.0.0.1":
43                                 type='c'
44                         else:
45                                 type='?'
46
47                         xid_stream = os.popen("vserver-info %s XID" % pid)
48                         xid = xid_stream.read()
49
50                         port_summary[port_key] = {'prot' : ns_fields[0], 
51                                                                           'src_port' : src_port, 
52                                                                           'dst_port' : dst_port, 
53                                                                           'slice' : xid[:-1], 
54                                                                           'type': type}
55         except:
56                 import traceback; traceback.print_exc()
57                 print line
58
59 ports = port_summary.keys()
60 ports.sort()
61 for port in ports:
62         print "%(prot)4s\t%(src_port)6s\t%(dst_port)6s\t%(slice)5s\t%(type)s" % port_summary[port]
63