Python 实现重启路由器

有一些服务,需要动态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”

下面关于Python的文章您也可能喜欢,不妨看看:

Python:在指定目录下查找满足条件的文件  http://www.linuxidc.com/Linux/2015-08/121283.htm

Python2.7.7源码分析  http://www.linuxidc.com/Linux/2015-08/121168.htm

无需操作系统直接运行 Python 代码  http://www.linuxidc.com/Linux/2015-05/117357.htm

CentOS上源码安装Python3.4  http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 语言的发展简史 http://www.linuxidc.com/Linux/2014-09/107206.htm

Python 的详细介绍请点这里
Python 的下载地址请点这里