# -*- coding: utf8 -*-
'''
Windows的netstat显示很不友好
-anO能只能显示pid,没法看到program name
-b能看出一些program name, 但很难看,而且一堆“无法获取所有权信息”
故, 拿python练练手, 好久没动过她了 ^_^
netstat -anO 和 tasklist /NH /FO CSV 通过pid把其他字段关联起来
'''
import os
taskinfo = os.popen('tasklist /NH /FO CSV')
task = {} # task['pid'] = 'program'
line = taskinfo.readline()
while line:
line = line.replace('"', '') # UNIX编程艺术就提到Windows的CSV垃圾了
aList = line.split(',')
task[aList[1]] = aList[0]
line = taskinfo.readline()
taskinfo.close()
netinfo = os.popen('netstat -naO')
# netstat 输出的3行垃圾信息
line = netinfo.readline()
line = netinfo.readline()
line = netinfo.readline()
# netstat 输出的头也不要
line = netinfo.readline()
line = netinfo.readline()
aList = ['Proto', 'Local Address', 'Foreign Address', 'State', 'PID', 'Program name']
print "%-8s%-25s%-25s%-15s%-8s\t%-s" %(aList[0], aList[1], aList[2],
aList[3],aList[4], aList[5])
while line:
aList = line.split()
if len(aList) == 4:
aList.append(aList[3])
aList[3] = ''
aList.append(task[aList[-1]])
print "%-8s%-25s%-25s%-15s%-8s\t%-s" %(aList[0], aList[1], aList[2],
aList[3],aList[4], aList[5])
line = netinfo.readline()
netinfo.close()
阅读(5987) | 评论(0) | 转发(0) |