刚好做运维平台,领导也有个要求是实时的查看,任意一台主机的端口开启状态,实际上我已经做了脚本发邮件每天diff的功能,但是为了方便展示还是写了这个脚本:
脚本内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | [root@mail python] # cat test1.py #coding=utf-8 #!/usr/bin/python import string import commands def getComStr(comand): try : stat,proStr = commands.getstatusoutput(comand) except : print "command %s execute failed, exit" % comand return proStr def filterList(): tmpStr = getComStr( "netstat -tpln" ) tmpList = tmpStr.split( "\n" ) List = tmpList[ 2 ::] newList = [] for i in List : val = i.split() del val[ 0 : 3 ] del val[ 1 : 3 ] valTmp = val[ 0 ].split( ":" ) val[ 0 ] = valTmp[ 1 ] valTmp = val[ 1 ].split( "/" ) val[ 1 ] = valTmp[ - 1 ] val = '------> ' .join(val) if ' _' not in val: newList.append(val) return newList if __name__ = = "__main__" : netInfo = filterList() appInfo = list ( set (netInfo)) appInfo.sort(key = netInfo.index) for i in appInfo: print i |
运行效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | [root@mail python] # python test1.py 22 - - - - - - > sshd 8888 - - - - - - > python 25 - - - - - - > master 4505 - - - - - - > python 4506 - - - - - - > python 4190 - - - - - - > cyrus - master 8000 - - - - - - > python 993 - - - - - - > dovecot 10050 - - - - - - > zabbix_agentd 5666 - - - - - - > nrpe 10051 - - - - - - > zabbix_server 995 - - - - - - > dovecot 199 - - - - - - > snmpd 10025 - - - - - - > master 110 - - - - - - > dovecot 143 - - - - - - > dovecot 783 - - - - - - > spamd - - - - - - > - d 80 - - - - - - > httpd |
当然也可以实现用列表的方式展示入库,然后前端读取,但是这里为了这本我直接用ajax的方式前端返回这个值,主要是结合了:ajax 然后用salt执行脚本返回值:
效果展示:
实际上实现方式很灵活,也可以入库然后结合echerts之类的画图软件,显示端口的思路。
本文转自 小罗ge11 51CTO博客,原文链接:http://blog.51cto.com/xiaoluoge/1637553,如需转载请自行联系原作者