Python 3 telnetlib 路由器重启
Python 3 telnetlib router reboot
我正在尝试编写一个 python 脚本来重启我的路由器。
我可以用普通的 telnet 做到这一点,但是,在 python 代码中,出于某种原因,除非我添加 tn.read_all() 到代码底部,重启操作不执行。
这是当前的工作代码:
import sys
import telnetlib
import time
HOST = "192.168.0.1"
password = "12345678"
try:
with telnetlib.Telnet(HOST,23,timeout=10) as tn:
print(tn.read_until(b'password:', 5))
tn.write((password + '\r\n').encode('ascii'))
print(tn.read_until(b'(conf)#', 5))
tn.write(('dev reboot' + '\r\n').encode('ascii'))
time.sleep(1)
print(tn.read_all().decode('ascii'))
except EOFError:
print("Unexpected response from router")
except ConnectionRefusedError:
print("Connection refused by router. Telnet enabled?")
except:
print("Error")
telnet 操作的正常输出是:
--------------------------------------------------------------------------------
Welcome To Use TP-Link COMMAND-LINE Interface Model.
--------------------------------------------------------------------------------
TP-Link(conf)#dev reboot
[ oal_sys_reboot ] 489: now sleep for 2 secs
TP-Link(conf)#killall: pppd: no process killed
保持read_all() 使操作超时并打印异常中定义的“错误”。
我想让它保持干净和简单。我怎样才能做到这一点?
显然,延迟不够,连接正在快速关闭。添加 read_all 使连接保持打开状态,因此在添加时执行命令。
解决方案是将延迟从 1 秒增加到 5 秒。
import sys
import telnetlib
import time
HOST = "192.168.0.1"
password = "12345678"
port = 23
try:
print('Opening Telnet Connection to Router.')
with telnetlib.Telnet(HOST,port,timeout=10) as tn:
tn.read_until(b'password:', 10)
print('Sending password to Router.')
tn.write((password + '\r\n').encode('ascii'))
time.sleep(1)
tn.read_until(b'(conf)#', 10)
print('Rebooting the Router!!!')
tn.write(('dev reboot' + '\r\n').encode('ascii'))
time.sleep(5)
except EOFError:
print("Unexpected response from router")
except ConnectionRefusedError:
print("Connection refused by router. Telnet enabled?")
except:
print("Error")
我正在尝试编写一个 python 脚本来重启我的路由器。
我可以用普通的 telnet 做到这一点,但是,在 python 代码中,出于某种原因,除非我添加 tn.read_all() 到代码底部,重启操作不执行。 这是当前的工作代码:
import sys
import telnetlib
import time
HOST = "192.168.0.1"
password = "12345678"
try:
with telnetlib.Telnet(HOST,23,timeout=10) as tn:
print(tn.read_until(b'password:', 5))
tn.write((password + '\r\n').encode('ascii'))
print(tn.read_until(b'(conf)#', 5))
tn.write(('dev reboot' + '\r\n').encode('ascii'))
time.sleep(1)
print(tn.read_all().decode('ascii'))
except EOFError:
print("Unexpected response from router")
except ConnectionRefusedError:
print("Connection refused by router. Telnet enabled?")
except:
print("Error")
telnet 操作的正常输出是:
--------------------------------------------------------------------------------
Welcome To Use TP-Link COMMAND-LINE Interface Model.
--------------------------------------------------------------------------------
TP-Link(conf)#dev reboot
[ oal_sys_reboot ] 489: now sleep for 2 secs
TP-Link(conf)#killall: pppd: no process killed
保持read_all() 使操作超时并打印异常中定义的“错误”。 我想让它保持干净和简单。我怎样才能做到这一点?
显然,延迟不够,连接正在快速关闭。添加 read_all 使连接保持打开状态,因此在添加时执行命令。 解决方案是将延迟从 1 秒增加到 5 秒。
import sys
import telnetlib
import time
HOST = "192.168.0.1"
password = "12345678"
port = 23
try:
print('Opening Telnet Connection to Router.')
with telnetlib.Telnet(HOST,port,timeout=10) as tn:
tn.read_until(b'password:', 10)
print('Sending password to Router.')
tn.write((password + '\r\n').encode('ascii'))
time.sleep(1)
tn.read_until(b'(conf)#', 10)
print('Rebooting the Router!!!')
tn.write(('dev reboot' + '\r\n').encode('ascii'))
time.sleep(5)
except EOFError:
print("Unexpected response from router")
except ConnectionRefusedError:
print("Connection refused by router. Telnet enabled?")
except:
print("Error")