因为工作需要,有时候在流控设备上查看一个ip地址流量比较大,需要知道这个这个ip地址对应的mac地址使用人是谁,我们有mac和使用人的excel表格,所以编写此脚本,可以查看任意设备接口连接情况
import telnetlib
import re
import os
import time
import subprocess
import pandas as pd
def telnet_switch(User,Password,Host,Mac):
tn=telnetlib.Telnet(Host)
# tn.set_debuglevel(2)
time.sleep(2)
tn.read_until(rb"Username:")
time.sleep(2)
tn.write(User.encode("ascii") + b"\r\n")
time.sleep(2)
tn.read_until(rb"Password:")
time.sleep(2)
tn.write(Password.encode("ascii") + b"\r\n")
time.sleep(5)
print("Password Success")
if Host in ["192.168.3.1","192.168.1.1","192.168.254.1"]:
tn.write(b"display mac-address %s" %Mac + b"\r\n")
time.sleep(5)
bb=tn.read_very_eager().decode('ascii')
with open("mac","w") as fm:
fm.write(bb)
cc=subprocess.Popen("cat mac | grep {0} | awk '{{print $3}}'".format(Mac.decode('utf-8')),stdout=subprocess.PIPE,shell=True)
data=cc.stdout.read().decode("utf-8").split("\n")
print(data)
else:
tn.write(b"display arp | in %s"%Mac + b"\r\n")
time.sleep(5)
bb=tn.read_very_eager().decode('ascii')
# bb= tn.read_all() read_eager
with open("mac","w") as fm:
fm.write(bb)
# cc=os.system("cat mac | grep {{0}} | awk '{{print $3}}'".format(Mac))
cc=subprocess.Popen("cat mac | grep {0} | awk '{{print $5}}'".format(Mac.decode('utf-8')),stdout=subprocess.PIPE,shell=True)
data=cc.stdout.read().decode("utf-8").split("\n")
print(data)
if len(data) == 3:
if re.match("^Wlan",data[1]):
print("此接口%s连接的是终端,并是无线接口"%data[1])
mac_user_excel(Mac,excel)
else:
dd=data[1].replace("GE","G").encode("utf-8")
time.sleep(5)
tn.write(b"display lldp neighbor interface %s" %dd + b"\r\n")
time.sleep(5)
ee=tn.read_very_eager().decode('ascii')
with open("lldp","w") as fm:
fm.write(ee)
ff=subprocess.Popen("cat lldp | grep 'System name' | awk -F':' '{{print $2}}'",stdout=subprocess.PIPE,shell=True)
gg=subprocess.Popen("cat lldp | grep 'Management address' | grep -v 'Management address type' |awk -F':' '{{print $2}}'",stdout=subprocess.PIPE,shell=True)
datagg=gg.stdout.read().decode("utf-8").split("\n")
dataff=ff.stdout.read().decode("utf-8").split("\n")
print(dataff,datagg)
if len(datagg[0]) !=0:
print("设备%s连接的网络设备信息如下:\n"%Host+"设备名称:",dataff[0]+"\n"+"IPadd:",datagg[0])
else:
print("此接口%s连接的是终端"%data[1])
mac_user_excel(Mac,excel)
else:
print("没有此%s地址"%Mac.decode("utf-8"))
def mac_user_excel(Mac,excel):
test_DELL=[]
test_keys=[]
test_values=[]
df=pd.read_excel(excel,sheet_name=("Dell笔记本"))
full_data=df.index.values
for i in full_data:
row_data=df.iloc[i,[4,10]].to_dict() #5--服务编号,10---使用人
test_DELL.append(row_data)
for i in test_DELL:
for j,k in i.items():
test_keys.append(i)
test_values.append(k)
if Mac.decode("utf-8") not in test_values:
print("没有此%s地址"%Mac.decode("utf-8"))
else:
mac_name=test_keys[test_values.index(Mac.decode("utf-8"))] #知道values找keys
print("MAC地址:%s"%mac_name["mac地址"],"\n使用人:%s"%mac_name["使用人"])
if __name__ == '__main__':
User="kangbao.wu"
Password="kangbao@wu"
Host=input("请输入需要登录的地址x.x.x.x:").strip()
Mac=input("请输入MAC地址xxxx-xxxx-xxxx:").encode("utf-8").strip()
excel="zichan.xlsx"
telnet_switch(User,Password,Host,Mac)
excel表格
运行结果: