我使用以下脚本使用Telnet重启我的路由器:
#!/usr/bin/env python
import os
import telnetlib
from time import sleep
host = "192.168.1.1"
user = "USER"
password = "PASSWORD"
cmd = "system restart"
tn = telnetlib.Telnet(host)
sleep(1)
tn.read_until("Login: ")
tn.write(user + "\n\r")
sleep(1)
tn.read_until("Password: ")
tn.write(password + "\n\r")
sleep(1)
tn.write(cmd + "\n\r")
Run Code Online (Sandbox Code Playgroud)
我不知道为什么,但从上面的代码中删除"\ r"使脚本无法正常工作.那么"\ r"在这个脚本中做什么以及何时一般使用"\ r"?
注意:我知道"回车",但仍然无法弄清楚它在我的脚本中的用途.我在Linux中运行此脚本.