如何通过telnetlib模块操作中兴设备
import telnetlib, re, os, threading, multiprocessing, datetime
import pandas as pd
from pandas
pd.set_option('display.width', None)
path = os.getcwd()
def telnet(host, port, username, password):
tn = telnetlib.Telnet(host, port, timeout=5)
tn.read_until(b"Username:")
tn.write(username.encode('ascii') + b"\n")
tn.read_until(b"Password:")
tn.write(password.encode('ascii') + b"\n")
tn.read_until(b"#", timeout=5)
tn.write(b"terminal length 0 \n")
tn.read_until(b"#")
return tn
def runcommands(host, port, username, password, command):
commands = command.split("\n")
output = ""
try:
tn = telnet(host, port, username, password)
for cmd in commands:
tn.write(cmd.encode('ascii') + b"\n")
output_tmp = tn.read_until(b"#", 5)
output += output_tmp.decode('ascii')
tn.close()
print(output)
except Exception as e:
print(f"命令执行失败,{e}")
try:
tn.close()
except Exception as e:
print("tn close failed")
return output
def showInterfaceBrief(host, port, username, password,command="show interface bri"):
output = runcommands(host, port, username, password, command)
outputs = output.splitlines()
msgs = []
for line in outputs:
pattern = r'(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(down|up)\s+(down|up)\s+(down|up)\s+(\S*)'
matchs = re.findall(pattern, line)
if matchs != []:
match = matchs[0]
if len(match) == 8:
desc = match[7]
else:
desc = ""
msgs.append({
'interface': match[0],
'portattribute': match[1],
'mode': match[2],
'bw(mbps)': match[3],
'admin_status': match[4],
'phy_status': match[5],
'prot_status': match[6],
'description': desc,
})
df = pd.DataFrame(msgs)
df["host"] = host
print(df)
return df
def showInterfaceDetail(host, port, username, password,command="show interface "):
try:
interfaces = showInterfaceBrief(host, port, username, password, "show interface bri")
interfaces["bw"] = None
interfaces["mtu"] = None
interfaces["In_Bytes"] = None
interfaces["E_Bytes"] = None
tn = telnet(host, port, username, password)
for interface in interfaces["interface"]:
cmd = command + interface
tn.write(cmd.encode('ascii') + b"\n")
output = tn.read_until(b"#", 5).decode("ascii")
pattern = r'BW\s+(\d+)'
match = int(re.findall(pattern, output, re.S)[0])
interfaces.loc[interfaces["interface"] == interface, "bw"] = match
pattern = r'MTU\s+(\d+)'
match = int(re.findall(pattern, output, re.S)[0])
interfaces.loc[interfaces["interface"] == interface, "mtu"] = match
pattern = r'In_Bytes\s+(\S+)\s+'
match = int(re.findall(pattern, output, re.S)[0])
interfaces.loc[interfaces["interface"] == interface, "In_Bytes"] = match
pattern = r'E_Bytes\s+(\S+)\s+'
match = int(re.findall(pattern, output, re.S)[0])
interfaces.loc[interfaces["interface"] == interface, "E_Bytes"] = match
tn.close()
print(interfaces)
except Exception as e:
print(f"命令执行失败,{e}")
try:
tn.close()
except Exception as e:
print("tn close failed")
return interfaces
def showIpInterfacesBrief(host, port, username, password, command="show ip int bri"):
output = runcommands(host, port, username, password, command)
outputs = output.splitlines()
msgs = []
for line in outputs:
pattern = r'(\S+)\s+(\S+)\s+(\S+)\s+(down|up)\s+(down|up)\s+(down|up)\s*'
matchs = re.findall(pattern, line)
if matchs != []:
match = matchs[0]
msgs.append({
'interface': match[0],
'ip_address': match[1],
'mask': match[2],
'admin_status': match[3],
'phy_status': match[4],
'prot_status': match[5]
})
df = pd.DataFrame(msgs)
df["host"] = host
print(df)
return df
def pingtest(host, port, username, password, command="ping 192.168.10.1"):
output = runcommands(host, port, username, password, command)
pattern = r'Success rate is (\d+)\s*'
matchs = int(re.findall(pattern, output)[0])
if matchs == 0:
print(f'host {host} {command} is failed')
else:
print(f'host {host} {command} is successed, the success rate is {matchs}')
def showIpOspfNeighbor(host, port, username, password, command="show ip ospf neighbor"):
output = runcommands(host, port, username, password, command)
pattern = r'\((\S+)\)'
routerid = re.search(pattern, output).group(1)
outputs = output.splitlines()
neighbors = []
for line in outputs:
pattern = r'(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)'
matchs = re.findall(pattern, line)
if matchs != []:
match = matchs[0]
neighbors.append({
'neighborid': match[0],
'pri': match[1],
'state': match[2],
'deadtime': match[3],
'address': match[4],
'interface': match[5]
})
df = pd.DataFrame(neighbors)
df["routerid"] = routerid
print(df)
return df
def showIpOspfInterfaces(host, port, username, password):
output = runcommands(host, port, username, password, "show ip ospf interface")
outputs = re.split(r'\n\s*\n', output.strip())
ospf_msgs = []
for msg in outputs:
if "xgei" in msg:
pattern = r'(\S*xgei\S+)\s+is\s(up|down)'
matchs = re.findall(pattern, msg)
interface = matchs[0][0]
status = matchs[0][1]
pattern = r'.*Address\s+(\S+)\s+'
matchs = re.findall(pattern, msg)
address = matchs[0]
pattern = r'.*the\sarea\s(\S+)\s'
matchs = re.search(pattern, msg)
area = matchs[0]
pattern = r'.*Network\sType\s(\S+)'
matchs = re.findall(pattern, msg)
networktype = matchs[0]
pattern = r'.*Authentication\sType\s(\S+)'
matchs = re.findall(pattern, msg)
authentication = matchs[0]
pattern = r'.*Hello\s(\d+),\sDead\s(\d+),\sRetransmit\s(\d+)'
matchs = re.findall(pattern, msg)
hello = matchs[0][0]
dead = matchs[0][1]
retransmit = matchs[0][2]
ospf_msgs.append({
"host": host,
"interface": interface,
"status": status,
"address": address,
"area": area,
"networktype": networktype,
"authentication": authentication,
"hello": hello,
"dead": dead,
"retransmit": retransmit
})
df = pd.DataFrame(ospf_msgs)
print(df)
return df
def checkInterfacesAndRepair(host, port, username, password, interfaces):
print("----------------before repair interface----------------")
interfaces_df = showInterfaceBrief(host, port, username, password)
for interface in interfaces:
status = interfaces_df.loc[interfaces_df["interface"] == interface, "admin_status"].values[0]
if status == "down":
runcommands(host, port, username, password, f"conf t\ninterface {interface}\nno shutdown\nexit\nexit\nwrite\n")
print(f"{host} interface {interface} is down, now set it up")
else:
print(f"{host} interface {interface} is not down")
interfaces_after_df = showInterfaceBrief(host, port, username, password)
print("----------------after repair interface----------------")
for interface in interfaces:
status = interfaces_df.loc[interfaces_df["interface"] == interface, "admin_status"].values[0]
if status == "up":
print(f"{host} interface {interface} is up after repaired")
else:
print(f"{host} interface {interface} is still down after repaired")
if __name__ == '__main__':
devices = [{"ip":"10.0.0.1","port":"23","username":"zte","password":"zte"},
{"ip":"10.0.0.2","port":"23","username":"zte","password":"zte"}]
for device in devices:
host = device["ip"]
port = device["port"]
username = device["username"]
password = device["password"]
print(f'{host}-{port}-{username}-{password}')
showIpOspfInterfaces(host, port, username, password)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步