首页 > 代码库 > python打印系统所有tcp,udp监听端口及服务
python打印系统所有tcp,udp监听端口及服务
#!/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监听端口及服务
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。