我正在尝试创建一个程序,目的是在网络中搜索特定的mac地址。
当我运行cisco命令“ show mac-address-table”时,它给出的输出已保存到MH2。 如果该输出为“ 000c”。 在它所有的输出被保存到一个txt文件中,我希望我能够根据使用的命令(显示mac地址表vs显示mac地址表)过滤并从其中提取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')
“如果((CPMAC)中的“ 000c.15”)”是我认为遇到问题的代码部分。 任何帮助表示赞赏!