6 # NOTE: '--inet' lists only ipv4 addresses.
7 ns = os.popen("ncontext --nid 1 --migrate -- vcontext --xid 1 --migrate -- netstat -apnlut --inet", 'r')
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(':')
16 port_key='%s-%s' % (src_port, dst_port)
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.
22 conn_state = ns_fields[5]
23 if ns_fields[0] == "tcp":
24 proc_field = ns_fields[6]
26 if conn_state == "ESTABLISHED":
27 proc_field = ns_fields[6]
29 proc_field = ns_fields[5]
32 (pid,procname)= proc_field.split('/')
34 # NOTE: without a PID there is no way to associate with an XID
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" ):
40 elif src_ip == "127.0.0.1":
42 elif src_ip != "0.0.0.0" and src_ip != "127.0.0.1":
47 xid_stream = os.popen("vserver-info %s XID" % pid)
48 xid = xid_stream.read()
50 port_summary[port_key] = {'prot' : ns_fields[0],
51 'src_port' : src_port,
52 'dst_port' : dst_port,
56 import traceback; traceback.print_exc()
59 ports = port_summary.keys()
62 print "%(prot)4s\t%(src_port)6s\t%(dst_port)6s\t%(slice)5s\t%(type)s" % port_summary[port]