Telnetlib 输出到文本文件以便稍后作为变量调用
2019-10-17
563
我有一个程序,我正在尝试创建它来在网络中搜索特定的 mac 地址。
当我运行 cisco 命令“show mac-address-table”时,它会给出保存到 MH2 的输出。如果该输出中有“000c.”,则所有输出都会保存到一个 txt 文件中,我希望能够根据所使用的命令(show mac address-table 与 show mac-address-table)过滤并从中提取 vlan,因为带有 mac 地址的行的 vlan 位置可能在左侧或右侧。我计划稍后弄清楚该部分,但目前看来我的脚本似乎没有读取文件(该文件正在获取正确的输出并且其中有一个“000c。”条目)我将输入以下代码:
#!/usr/bin/env python3
from time import sleep
import telnetlib
from getpass import getpass
# f is the .txt document that lists the IP's we'll be using.
f = open("devicess.txt")
#
username = input("please provide your username:")
password = getpass()
#
for line in f:
device = (line)
print('Starting to collect information, please wait')
#For those devices in the above list, connect and run the below commands
def loopstart():
for device in f:
tn = telnetlib.Telnet()
tn.open(device, 23, 20)
#Remove # in the line below for debug
#tn.set_debuglevel(2000)
tn.read_until(b"Username:", timeout = 20)
sleep(.25)
tn.write(str(username + "\n").encode("ascii"))
sleep(.25)
tn.read_until(b"Password: ", timeout = 10)
sleep(.25)
tn.write((password + "\n").encode("ascii"))
sleep(.25)
#####################################
#Verify Login attempt below #
#####################################
try:
enablemode = tn.read_until(b"#")
if (b"FAIL") in enablemode:
print("Bad credentials to " + device)
tn.close()
sleep(.5)
elif (b"fail") in enablemode:
print("Bad credentials to " + device)
tn.close()
sleep(.5)
elif (b"#") in enablemode:
print("connection established to " + device)
try:
tn.write(str("show mac address-table | include 000c.\n").encode('ascii'))
sleep(2)
MH2 = tn.read_very_eager()
if (b"000c.15") in MH2:
try:
sleep(.5)
mactable = open("mactable.txt", "rb+")
mactable.seek(0)
mactable.write(MH2)
mactable.truncate()
OP1 = mactable.read
for line in OP1():
CPMAC = (line)
try:
if (b"000c.15") in CPMAC:
print("line 70 in use")
print((CPMAC) + " this is what vlan the cyber power device should be on")
tn.write(str("show interface vlan" + (CPMAC[:6]) + "\n")).encode("ascii")
tn.read_until(b"Internet Address")
tn.close()
elif (str("All")) in (CPMAC):
print ("CPU has matching MAC, moving to next device")
tn.close()
else:
print("No Cyber power device found on " + device)
tn.close()
except EOFError as e:
print("could not pull vlan from output")
except EOFError as e:
print("unidentified issue")
#Execute the following commands in case of invalid command input
elif (b"Invalid") in MH2:
sleep(.5)
try:
tn.write(str("show mac-address-table | in 000c.\n").encode('ascii'))
sleep(2)
MH3 = tn.read_very_eager()
if (b"000c.15") in MH3:
print("Line 90 in use")
try:
sleep(.5)
mactable = open("mactable.txt", "r+")
mactable.seek(0)
mactable.write(str(MH3))
OP2 = (mactable.read())
print (type(OP2))
mactable.truncate()
for line in OP2():
CPMAC = (line)
try:
if ("000c.15") in (CPMAC):
print((CPMAC) + " this is what vlan the cyber power device should be on")
tn.write(str("show interface vlan" + (CPMAC[:6])+ "\n").encode("ascii"))
tn.read_until(b"Internet Address")
tn.close()
elif (str("All")) in (CPMAC):
print ("CPU has matching MAC, moving to next device")
tn.close()
else:
print("No Cyber power device found on " + device)
tn.close()
except EOFError as e:
print("could not pull vlan from output")
except EOFError as e:
print("unidentified issue")
elif (b"000c.15") not in MH3:
print ("Cyber power device not found, moving to next device.")
tn.close()
else:
print("Unknown Error")
tn.close()
##############################
# Logout commands #
##############################
except EOFError as e:
print("Connection closed to " + device)
else:
tn.write(str("exit\n").encode('ascii'))
tn.write(str("exit\n").encode('ascii'))
tn.close()
print(tn.read_all().decode('ascii'))
except EOFError as e:
print ("unknown error")
else:
tn.close()
except EOFError as e:
print("Connection closed to " + device)
except Exception as exception:
print(exception, False)
tn.close()
loopstart()
print('script complete')
“if ("000c.15") in (CPMAC)”是我认为我遇到麻烦的代码部分。任何帮助都非常感谢!
2个回答
不太确定您要实现什么,但请检查行
OP1 = mactable.read
read 是一个应写为
OP1 = mactable.read()
Bassosimons
2019-10-18
以下是迄今为止对我有用的方法,我可以运行命令“show mac address-table”,获取该输出并将其放入文本文件中,逐行搜索输出中的 000c.15,并在以后使用该行进行进一步输出。我认为重要的是在将输出(字节)写入文本文件之前将其解码为字符串。此外,使用 seek(0) 函数有助于在开始阅读之前将我带回到 txt 文件的开头。Line.strip 似乎可以消除所有被解释为行的空白。最后一个不是 100% 确定。仍然无法让该代码发送命令,但我至少取得了进展。感谢大家的帮助。
if (b"000c.15") in MH2:
print("000c.15 in MH2, line 57")
try:
print ("line 59")
sleep(.5)
mactable = open("mactable.txt", "w+")
mactable.seek(0)
mactable.write(MH2.decode('utf-8'))
mactable.truncate()
mactable.seek(0)
OP1 = mactable.readlines()
for line in OP1:
line = line.strip()
CPMAC = line
andrew lee
2019-10-22