码迷,mamicode.com
首页 > 编程语言 > 详细

python打印系统所有tcp,udp监听端口及服务

时间:2017-03-12 16:54:15      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:ring   append   int   lis   val   color   span   监听   output   

#!/usr/local/bin/python3
#coding:utf-8

import string, subprocess

tcptmpStr = ((subprocess.check_output(["netstat", "-ntlp"])).decode(utf-8)).strip()
udptmpStr = ((subprocess.check_output(["netstat", "-nulp"])).decode(utf-8)).strip()
#get tcp port and service

def getTCPservice(tcptmpStr):
        tmpList = tcptmpStr.split("\n")
#        del tmpList[0:2]
        newList = []
        
        for i in tmpList:
            val = i.split()
            del val[0:3]
            del val[1:3]
            valTmp = (val[0].split(":"))[-1]
            val[0] = valTmp
            valTmp = val[1].split(/)
            val[1] = valTmp[-1]
            val =  .join(val)
            newList.append(val)
        return newList
    
#get udp port and service 

def getUDPservice(udptmpStr):        
        tmpList = udptmpStr.split("\n")
        del tmpList[0:2]
        newList = []
        
        for i in tmpList:
            val = i.split()
            del val[0:3]
            del val[1]
            valTmp = (val[0].split(":"))[-1]
            val[0] = valTmp
            valTmp = val[1].split(/)
            val[1] = valTmp[-1]
            val =  .join(val)
            newList.append(val)
        return newList

#def tcpService():     
for i in getTCPservice(tcptmpStr):
    val = i.split( , 1)
    port, app = val
    print(port, app) 

#def udpService():
for i in getUDPservice(udptmpStr):
    val = i.split( , 1)
    port, app = val
    print(port, app)
        
  

END!

python打印系统所有tcp,udp监听端口及服务

标签:ring   append   int   lis   val   color   span   监听   output   

原文地址:http://www.cnblogs.com/changbo/p/6538065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!