python读取交换机
python如何读取交换机的端口信息和转发表信息我只是路过打酱油的。 我只是路过打酱油的。 只是来看看!
python如何读取交换 :lol:.........大召唤术:我爱鱼C,鱼币来..... .........大召唤术:我爱鱼C,鱼币来..... 什么意思?用python写程序在电脑运行,通过物理连接到交换机读取信息? 交换机里有一个arp表,利用arp表可以找到与其相连的交换机的mac地址,然后再从mac端口对应表里找出端口
def getTrunk(ip,port,agent,communication):
macAddr = []
macList = []
portList = []
macStrList = []
linkPort = []
oid1 = (1,3,6,1,2,1,4,22,1,2)#ARP表oid
oid2 = (1,3,6,1,2,1,17,4,3,1,2)#mac端口对应表lid
gen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBinds = gen.nextCmd(
cmdgen.CommunityData(agent, communication,1),
cmdgen.UdpTransportTarget((ip,port)),
oid1,
)
#获取ARP表里的mac地址
for varBind in varBinds:
for name,val in varBind:
macAddr.append(val.prettyPrint(0))
gen.ignoreNonIncreasingOid = True#让oid可以非递增
errorIndication, errorStatus, errorIndex, varBinds = gen.nextCmd(
cmdgen.CommunityData(agent, communication,1),
cmdgen.UdpTransportTarget((ip,port)),
oid2,
)
#获取mac端口对应关系
for varBind in varBinds:
for name,val in varBind:
macList.append(name[-6:])
portList.append(val.prettyPrint())
#将十进制mac地址转化为16进制的字符串
for item in macList:
temp = ''
for part in item:
temp += str(hex(int(part)))
temp = '0x' + temp
macStrList.append(temp)
for item in macAddr:#获取端口号
if item in macStrList:
index = macStrList.index(item)
linkPort.append(portList)
#去除列表中的重复元素
linkPort = {}.fromkeys(linkPort).keys()#linkPort = list(set(linkPort))
return linkPort
我只是来混个鱼币。。 很牛逼的样子! 没办法啊 帮顶~~~~ Thanks for your fish coin. loop "学在苦中求,艺在勤中练" 共勉 高端 {:5_90:} 楼主有解决方案了吗,求分享 我来看看