收集服务器基本信息

#!/usr/bin/python
# coding: utf-8
import re, os


DiamondPath = "cat /root/diamond-v1.100.0010-SNAPSHOT/diamond.yaml"
user = "ubuntu"
keyPath = "1.key"
sshCmd = "ssh -i {key} ubuntu@{ip} sudo "

PortDict = {}
result = re.findall("- ip:[ \t]*(.*)", os.popen(DiamondPath).read())
grepPort = "|grep \'{port}\'"
newShell = "netstat -tnlp| grep -v 'Program'"

for ip in result:
    set01 = set()
    Ip = sshCmd.format(key=keyPath,ip=ip)
    print("*************" + ip + "*************")
    PortNetstat = Ip + newShell
    PortGrep = Ip + newShell + grepPort
    ServiceNam = re.findall(".*/(.*)", os.popen(PortNetstat).read().strip())
    for i in ServiceNam:
        set01.add(i.strip())
    setList = list(set01)
    for name in range(len(setList)):
        PortCmd = PortGrep.format(port=setList[name])
        PortResult = list(set(re.findall(".*:(\d+)", os.popen(PortCmd).read())))
        PortDict[setList[name]] = PortResult

    for k, v in PortDict.items():
        print(k, v)

 

posted @ 2020-06-09 16:06  Wshile  阅读(195)  评论(0编辑  收藏  举报