代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Python
】
数通设备配置文件备份
作者:
guoyh
/ 发布于
2012/3/6
/
914
<div>coding:utf-8</div> <div>!/usr/bin/python</div> <div>'''</div> <div>程序环境:linux + python + pexpect + tftp server</div> <div>功能: 自动登录网络设备备份配置文件,</div> <div>些程序版本: version 0.1 基本实现功能,供参考</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>当前最高版本 version 0.3</div> <div>编写:</div> <div>'''</div> import pexpect</div> import datetime</div> import time</div> <div>def getToday():</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return datetime.date.today()</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div>def telnet_hw3552(ip,login,passwd,su_passwd):</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>try:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo = pexpect.spawn('/usr/bin/telnet %s' % (ip))</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>index = foo.expect(['sername:', 'assword:'])<span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if index == 0:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(login)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect("assword:")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(passwd)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>#print(foo.before,foo.after)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>elif index == 1:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(passwd)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect(">")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("super")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>#print("suerp--->",foo.before,foo.after)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect("assword:")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(su_passwd)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>#print("super pwd ok")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect(">")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("tftp 10.241.11.115 put %s %s " % ("vrpcfg.cfg",ip+"_hw_"+str(getToday())+".cfg"))</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>index=foo.expect(["successfully","Error"])</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if index == 1:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(" ")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect(">")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("tftp 10.241.11.115 put %s %s " % ("vrpcfg.zip",ip+"_hw_"+str(getToday())+".zip"))</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("quit")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>except pexpect.EOF:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.close()</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>else:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.close<span class="Apple-tab-span" style="white-space:pre"> </span></div> <div></div> <div>思科ios系统交换机</div> <div>def telnet_ciscoios(ip,login,passwd,su_passwd):</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>try:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo = pexpect.spawn('/usr/bin/telnet %s' % (ip))</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>index = foo.expect(['sername:', 'assword:'])<span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if index == 0:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(login)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect("assword:")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(passwd)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>elif index == 1:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(passwd)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect(">")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("en")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect("assword:")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(su_passwd)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect("#")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("copy running-config tftp")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect(".*remote.*")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("%s" % ("10.241.11.115"))</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect(".*filename.*")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("%s" % (ip+"_ciscoIos_"+str(getToday())+"_runningconfig.cfg"))</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect("#")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("exit")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>except pexpect.EOF:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.close()</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>else:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.close</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div>h3c防火墙</div> <div>def telnet_h3cfirewallf1000(ip,login,passwd,su_passwd):</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>try:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo = pexpect.spawn('/usr/bin/telnet %s' % (ip))</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>index = foo.expect(['sername:', 'assword:'])<span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if index == 0:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(login)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect("assword:")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(passwd)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>elif index == 1:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(passwd)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect(">")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("tftp 10.241.11.115 put %s %s " % ("startup.cfg",ip+"_h3cf1000_"+str(getToday())+"_startup.cfg"))</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect(">")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("tftp 10.241.11.115 put %s %s " % ("system.xml",ip+"_h3cf1000_"+str(getToday())+"_system.xml"))</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect(">")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("quit")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>except pexpect.EOF:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.close()</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>else:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.close<span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div>netscreen firewall</div> <div>def telnet_netscren(ip,login,passwd,su_passwd):</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>try:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo = pexpect.spawn('/usr/bin/telnet %s' % (ip))</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>index = foo.expect(['login:', 'assword:'])<span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if index == 0:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(login)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect("assword:")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(passwd)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>elif index == 1:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(passwd)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect(">")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline(su_passwd)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect(">")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("save config to tftp 10.241.11.115 %s" % (ip+"_netscreen_"+str(getToday())+".cfg"))</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect("Succeeded")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect(">")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("exit")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>#foo.expect(" save\? [y]/n")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.expect(".*save.*")</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.sendline("Y")<span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>except pexpect.EOF:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.close()</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>else:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>foo.close<span class="Apple-tab-span" style="white-space:pre"> </span></div> <div></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div>'''函数使用'''</div> <div></div> <div>telnet_hw3552("10.241.11.27","admin","******","#######") #cfg</div> <div>telnet_hw3552("10.241.11.28","admin","******","#######")</div> <div>telnet_hw3552("10.23.98.100","admin","******","#######") #zip</div> <div>telnet_hw3552("10.23.98.101","admin","******","#######")</div> <div>telnet_netscren("10.203.10.167","netscreenroot","#######","#######") #netscreen</div> <div>telnet_netscren("10.203.10.168","netscreenroot","#######","#######")</div> <div>telnet_h3cfirewallf1000("10.241.11.124","admin","#######","") #h3c firewal f1000</div> <div>telnet_h3cfirewallf1000("10.241.11.125","admin","#######","")</div> <div>telnet_hw3552("10.221.103.16","admin","******","#######") #hw 8505</div> <div>telnet_ciscoios("10.223.10.23","admin","******","#######") #cisco switch io</div>
试试其它关键字
数通设备配置文件备份
同语言下
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
guoyh
贡献的其它代码
(
5
)
.
网页变模糊
.
数通设备端口使用情况监控程序
.
读取plist中的值
.
多任务时,如何限制启动线程数实例
.
数通设备配置文件备份
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3