python工具--获取盛科交换机端口模块类型,波长,传输距离等信息

个人博客地址

http://www.darkghost.life

交换机端口模块信息对应的OID节点为各厂商私有节点,获取其他厂商信息需要把OID进行替换

 1 #! /usr/bin/env python
 2 #-*-coding:utf-8-*-
 3 import re,signal,os,subprocess,sys
 4 class module:
 5     def __init__(self,ip,community):
 6         self.p = 'snmpwalk -v2c -c %s %s 1.3.6.1.4.1.27975.37.1.10.1.1.1'%(community,ip)
 7         self.ip = ip
 8         self.community = community
 9     def cmd(self,p):
10         m_type = 'snmpwalk -v2c -c %s %s 1.3.6.1.4.1.27975.37.1.10.1.1.1.%s'%(self.community,self.ip,p)
11         nm = 'snmpwalk -v2c -c %s %s 1.3.6.1.4.1.27975.37.1.10.1.1.5.%s'%(self.community,self.ip,p)
12         length = 'snmpwalk -v2c -c %s %s 1.3.6.1.4.1.27975.37.1.10.1.1.6.%s'%(self.community,self.ip,p)
13         pn = 'snmpwalk -v2c -c %s %s 1.3.6.1.4.1.27975.37.1.10.1.1.3.%s'%(self.community,self.ip,p)
14         return m_type,nm,length,pn
15     def port(self):
16         module_type = subprocess.Popen(self.p,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]
17         li = []
18         module_type= module_type.decode('utf8')
19         module_type = module_type.replace('SNMPv2-SMI::enterprises.27975.37.1.10.1.1.1.','').split('\n')
20         for x in module_type:
21             if len(x)>10:
22                 li.append(x.split('=')[0].strip())
23         return li
24     def value(self,cmd,t_ype):
25         li = []
26         value =  subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]
27         value = value.decode('utf8')
28         value = re.findall('=.+',value)[0].split(':')[1].strip()
29         li.append({t_ype:value})
30         return li
31     def get(self):
32         dic = {}
33         getport = self.port()
34         m_typeli = []
35         nmli = []
36         lengthli=[]
37         pnli=[]
38         for x in getport:
39             m_type,nm,length,pn = self.cmd(x)
40             m_typeli.append(m_type)
41             nmli.append(nm)
42             lengthli.append(length)
43             pnli.append(pn)
44         for t in m_typeli:
45             index = m_typeli.index(t)
46             key = getport[index]
47             ret = self.value(t,'类型')
48             dic[key]=ret
49         for n in nmli:
50             index = nmli.index(n)
51             key = getport[index]
52             ret = self.value(n,'波长')
53             dic[key].append(ret)
54         for l in lengthli:
55             index = lengthli.index(l)
56             key = getport[index]
57             ret = self.value(l,'传输距离')
58             dic[key].append(ret)
59         for pn in pnli:
60             index = pnli.index(pn)
61             key = getport[index]
62             ret = self.value(pn,'PN')
63             dic[key].append(ret)
64         return dic
65 if __name__  == "__main__":
66     try:
67         host = sys.argv[1]
68         community = sys.argv[2]
69         sw = module(host,community)
70         ret = sw.get()
71         print('----------------------------------%s 端口模块信息------------------------------------'%host)
72         for x in ret:
73             print('port%s'%x,ret[x])
74     except:
75         print('use:')
76         print('    ./centecosmodule.py host community')
77         print('like:')
78         print('    ./centecosmodule.py 10.0.3.102 a000123A')

 

posted @ 2021-07-03 15:39  无限's-blog  阅读(258)  评论(0编辑  收藏  举报