- 论坛徽章:
- 14
|
本帖最后由 reb00t 于 2015-02-15 11:01 编辑
功力不够,这代码我都看吐了。- #!/usr/bin/env python
- import os
- import sys
- import re
- d = {}
- ddir = "/home/enb"
- sp = []
- def portlist(ddir,d):
- for root,dirs,files in os.walk(ddir):
- for name in files:
- fp = os.path.join(root, name)
- f = open(fp,'r+')
- text = f.read().strip()
- #pl.append(text)
- d[name] = text
- def portdata(d,fname='1'):
- with open(fname) as f:
- for line in f:
- m = re.match(r"^alias\s+(\w+)='ssh.*-p\s+(\d+[^\s]+).*", line)
- if m:
- k = m.group(1)
- v= m.group(2)
- d[k]=v
- def netport(sp):
- cmd = "netstat -tna|grep \"localhost:\""
- s=""
- port = os.popen(cmd).readlines()
- #print port,
- for l in port:
- s=l.split()[3].split(':')[1]
- sp.append(s)
- def main():
- portlist(ddir,d)
- portdata(d)
- netport(sp)
- #sp = ['55001','55002','55003','55004']
- print "%-25s\t%-15s\t%-5s\n" %("host", "port", "status")
- print "-"*60
- for k,v in d.items():
- if v in sp:
- print "%-25s\t%-15s\t%-5s" %(k,v,"ok")
- else:
- print "%-25s\t%-15s\t%-5s" %(k,v,"off")
- if __name__ == '__main__':
- main()
- 结果:
- host port status
- ------------------------------------------------------------
- enb.port.enb74324@beijing 64650 off
- enb.port.enb24042@shanghai 64649 off
- hp8 55000 off
- yok 55002 ok
- edo 55003 ok
- tok 55004 ok
- shi 55001 ok
复制代码 |
|