telnetlib read_until 无法工作,因为不确定期望的主机名
telnetlib read_until can't work as not sure what hostname to expect
我的程序需要 telnet 到可疑设备列表,但是它们的所有主机名都不同,但是它们每次都遵循相同的规则,我将在下面附上。
我的问题是 telnetlib
在尝试输入用户名之前需要一个 read_until 参数,在我的例子中我不知道那个主机名是什么。
我尝试将 read_all 保存为变量并读取它,希望提取主机名,但命令按预期挂起
代码
import telnetlib
username = 'admin'
password = 'password'
text = []
def telnet2():
tn = telnetlib.Telnet('10.10.10.199')
tn.write(username + "\r\n")
if password:
tn.read_until('Password: ')
tn.write(password + '\r\n')
tn.write('enable \r\n')
tn.write('show poe status \r\n')
tn.write('show mac-address-table \r\n')
tn.write('exit \r\n')
text.append(tn.read_all())
telnet2()
可能的主机名
Switch_100-110
额外
在这种情况下,主机名是 Switch_109,但如果我没有手动连接检查,我不会知道。
** 错误信息 **
`File "connect2.py", line 18, in telnet2
print(tn.read_all())
File "/usr/lib/python2.7/telnetlib.py", line 385, in read_all
self.fill_rawq()
File "/usr/lib/python2.7/telnetlib.py", line 576, in fill_rawq
buf = self.sock.recv(50)`
我最终不得不包括一个睡眠并增加调试缓冲区大小以适应缓冲区接收的数据量,这对我自己来说可能无法 100% 工作,因为 mac-地址-table 会在用户连接的一整天内发生变化,但是我只是在寻找一个单独的 MAC 地址,它位于与最终用户不同的界面上,所以没问题
import time
import telnetlib
username = 'admin'
password = 'password'
text = []
my_text = ''
def telnet2():
tn = telnetlib.Telnet('10.10.10.199')
tn.read_until('switch_')
tn.write(username + "\r\n")
if password:
tn.read_until('Password: ')
tn.write(password + '\r\n')
tn.write('enable \r\n')
tn.write('show poe status \r\n')
time.sleep(1)
tn.write('\r\n')
tn.write('enable \r\n')
tn.write('show mac-address-table \r\n')
tn.write('\r\n')
time.sleep(2) # handy for not overloading the buffer straight away/accommodating for time delay due to latency
tn.write('exit \r\n')
tn.write('exit \r\n')
my_text = tn.set_debuglevel(40000) # by changing the debuglevel it will take in more data
my_text = tn.read_all()
print(my_text)
telnet2()
我的程序需要 telnet 到可疑设备列表,但是它们的所有主机名都不同,但是它们每次都遵循相同的规则,我将在下面附上。
我的问题是 telnetlib
在尝试输入用户名之前需要一个 read_until 参数,在我的例子中我不知道那个主机名是什么。
我尝试将 read_all 保存为变量并读取它,希望提取主机名,但命令按预期挂起
代码
import telnetlib
username = 'admin'
password = 'password'
text = []
def telnet2():
tn = telnetlib.Telnet('10.10.10.199')
tn.write(username + "\r\n")
if password:
tn.read_until('Password: ')
tn.write(password + '\r\n')
tn.write('enable \r\n')
tn.write('show poe status \r\n')
tn.write('show mac-address-table \r\n')
tn.write('exit \r\n')
text.append(tn.read_all())
telnet2()
可能的主机名
Switch_100-110
额外
在这种情况下,主机名是 Switch_109,但如果我没有手动连接检查,我不会知道。
** 错误信息 **
`File "connect2.py", line 18, in telnet2
print(tn.read_all())
File "/usr/lib/python2.7/telnetlib.py", line 385, in read_all
self.fill_rawq()
File "/usr/lib/python2.7/telnetlib.py", line 576, in fill_rawq
buf = self.sock.recv(50)`
我最终不得不包括一个睡眠并增加调试缓冲区大小以适应缓冲区接收的数据量,这对我自己来说可能无法 100% 工作,因为 mac-地址-table 会在用户连接的一整天内发生变化,但是我只是在寻找一个单独的 MAC 地址,它位于与最终用户不同的界面上,所以没问题
import time
import telnetlib
username = 'admin'
password = 'password'
text = []
my_text = ''
def telnet2():
tn = telnetlib.Telnet('10.10.10.199')
tn.read_until('switch_')
tn.write(username + "\r\n")
if password:
tn.read_until('Password: ')
tn.write(password + '\r\n')
tn.write('enable \r\n')
tn.write('show poe status \r\n')
time.sleep(1)
tn.write('\r\n')
tn.write('enable \r\n')
tn.write('show mac-address-table \r\n')
tn.write('\r\n')
time.sleep(2) # handy for not overloading the buffer straight away/accommodating for time delay due to latency
tn.write('exit \r\n')
tn.write('exit \r\n')
my_text = tn.set_debuglevel(40000) # by changing the debuglevel it will take in more data
my_text = tn.read_all()
print(my_text)
telnet2()