我有一个程序,我正试图创建的目的是搜索特定的mac地址的网络。你知道吗
当我运行cisco命令“showmac address table”时,它会给出保存到MH2的输出。如果输出中有“000c”,那么所有的输出都被保存到一个txt文件中,我希望我能够根据使用的命令(show mac address table vs 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')
(CPMAC)中的“if(”000c.15“)”是我认为有问题的代码部分。感谢您的帮助!你知道吗
因此,以下是迄今为止对我有效的方法,我能够运行命令“show mac address table”,将输出放入文本文件,逐行搜索000c.15的输出,并在以后的过程中使用该行进一步输出。我认为重要的是在将输出(字节)写入文本文件之前将其解码为字符串。另外,使用seek(0)函数有助于在开始读取之前将我带回txt文件的开头。线条.strip似乎摆脱了所有被解读为线条的空白。最后一个不是100%确定。仍然有问题,让代码发送命令,但我正在取得进展,至少。谢谢大家的帮助。你知道吗
不确定您想要实现什么,但是检查一下
OP1 = mactable.read
read行是一个函数,应该写成OP1 = mactable.read()
相关问题 更多 >
编程相关推荐