Talking about Seatlocation

最初想法

日常故障排查的过程中,需要定位某个IP对应的同事,所处位置信息。网络人员通过交换机端的ARP信息,查询对象的mac地址,再通过mac地址去查询对应信息。后期发现,此类 查询过程多为重复动作,因此想要寻求方法让系统去做查询的过程。

流程图

入库

显而易见的是,最重要的部分就是两张表。
座位号对应交换机接口的信息,由于很少变动并且相对固定,因此需要人工手动统计。而交换机mac地址与交换机接口对应表则由于其变化性、复杂性等等。需要利用脚本去执行并且更新:
如:
def Initmacinterfacetable():
id=1
switchs=[‘10.101.20.231′,’10.101.20.232′,’10.101.20.233′,’10.101.20.234′,’10.101.20.235′,’10.101.20.236’]
for switch in switchs:
for errorIndication, errorStatus, \
errorIndex, varBinds in bulkCmd(
SnmpEngine(),
CommunityData(‘xindalou-idc’),
UdpTransportTarget((switch, 161)),
ContextData(),
0, 20, # GETBULK specific: request up to 50 OIDs in a single response
ObjectType(ObjectIdentity(‘.1.3.6.1.2.1.17.4.3.1.2’)),
lookupMib=False, lexicographicMode=False,ignoreNonIncreasingOid = True):

        if errorIndication:
            print(errorIndication)
            break

        elif errorStatus:
            print('%s at %s' % (errorStatus.prettyPrint(),
                                errorIndex and varBinds[int(errorIndex)-1][0] or '?'))
            break
        else:
            try:
                conn = dbinfo()
                conncur = conn.cursor()
                for varBind in varBinds:
                    oidmac=[x.prettyPrint() for x in varBind]
                    macaddress=oidmac[0][23:]
                    num=int(oidmac[1])
                    if num > 52:
                        break
                    else:
                        interface=switch+'-'+'G0/0/'+oidmac[1]
                        sql_mac= "insert into macinterface(id,SWinterface,mac) value('%s','%s','%s')" % (id,interface,macaddress)
                        conncur.execute(sql_mac)
                        id=id+1
                conn.commit()
                conn.close()
            except Exception as e:
                print(e)

查库

此部分理论方便相对简单,因此直接给出部分代码:

def macinfo(IP):
conn = dbinfo()
conncur = conn.cursor()
connsql = “select macaddress from tcddhcp WHERE ipaddress = ‘%s'” % IP
conncur.execute(connsql)
macaddress = conncur.fetchall()
conn.commit()
conn.close()
info = macaddress[0]
mac= info[‘macaddress’]
mac_list = mac.split(‘-‘)
mac_int_list = [int(i, 16) for i in mac_list]
mac_str_list = [str(i) for i in mac_int_list]
mac_int = ‘.’.join(mac_str_list)
if mac_int:
return mac_int
else:
return 2

def getSWinterface(mac):
conn=dbinfo()
conncur = conn.cursor()
sql_interface=”select SWinterface from macinterface WHERE mac = ‘%s'” % mac
conncur.execute(sql_interface)
swinterfacedic = conncur.fetchall()
swinterface=swinterfacedic[0][‘SWinterface’]
conn.commit()
conn.close()
if swinterface:
return swinterface
else:
return 2

发表评论

电子邮件地址不会被公开。 必填项已用*标注