有一些服务,需要动态IP,所以我们用重启路由器的方法实现。人工重启不可选,用定时脚本执行即可。贴代码,每种路由器,提示不一样。需要路由器有telnet功能才行。
#!/usr/bin/env python # -*- coding: utf-8 -*- import telnetlib HOST = "192.168.1.1" USER = "admin" PASS = "admin" router = telnetlib.Telnet(HOST) router.set_debuglevel(2) router.read_until("Username:", 12) router.write(USER + "\r\n") router.read_until("Password:", 12) router.write(PASS + "\r\n") router.read_until("TP-LINK > ", 12) router.write("enable\r\n") router.read_until("Enter password: ", 12) router.write(PASS + "\r\n") router.read_until("TP-LINK # ", 12) router.write("sys reboot\r\n") router.read_until("Continue?", 12) router.write("Y\r\n") router.close() print "Done"