zoukankan      html  css  js  c++  java
  • 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("
    ")
    #        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("
    ")
            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!

  • 相关阅读:
    CSS知识点总结[部分]
    前端知识日常总结
    上传第三方jar包到nexus
    maven命令
    mac安装并配置nexus3.5.1版本
    前端解决跨域问题
    Package.json 属性说明
    nodejs日常总结
    log4j.properties总结
    Spring整合Hibernate
  • 原文地址:https://www.cnblogs.com/changbo/p/6538065.html
Copyright © 2011-2022 走看看