1、利用Python通过telnet,ssh连接交换机、路由器等设备配置备份数据库设计:(主要存储设备信息如IP等)c Vimaaeddevies-Hei diSQL 9.1.0.4867.主机127,0 0.1 款据库device|查询|o文件搞辑搜索工具芾助独炉|白的尊昌修由 旧、E蚓网w GX *L:*囹扇曲密工s 期:50目.1 76.0 KiBi sreas1 6.0 KiB hosts84.0 KxBJ ftod l s48.0 KiBJ protocol s1 6.0 KiBJ type1 6 0 l aBvendors1 6 0 MB下 i nform at ion_ schwa
2、mysql per fora anc e_schea a3 test1 60 0 KiB名称修改时间引擎 I注移I类型数据条数I大小81 6 0 KiB20 1 5-0 4-29 1 9InnoDBTabl e5164 0 KiB20 1 5-0 9-0 5 1 6.InnoDBTabl emodel s5048 0 KiB20 1 5-0 4-29 20.IimoDBTabl eprotocol s21 6.0 KiB20 1 5-0 9-0 5 1 5,InnoDBTabl etypes1 01 6 0 KiB20 1 5-0 4-29 20.InnoDBTabl evendors81 6
3、0 KiB20 1 5-0 4-29 1 9 一InnoDBTabl edi SQL 9.1.0.4867文件 爆箱 整索 工具 帮助4义寅才1。堂i-1才砧眼Ml X,i*E;O1 3;n:i 主机 is70 0.1|数据座dovyi表:ht|髓数掘|查询|e1白/Inn 缺 gd devicesN areas1 76 0 KiB1 6.0 KiB基本。添加 令引除清除迭顼1索引外键卜般分区L CRE ATE代码|匚.AU E R代码|i.键名1字段 1关联表 1外联字段FK_hosts_ai-easTK-hests-model sarea_id areas area_idhost-m d
4、model s model ednia二hosts1r jdel s 4c:Lt!,U_j protocol s 1 6.0 KiB|types 1 6 0 KiBvendors 1 6 0 iBinformation schema 1 60 0 KiBmysql p erorftance_sche a字段:。添加涮?3上,向下test*1名称树麟空1长度/设置无符号的|允许皿|填雪|默认|注释1 host_idIMTSAU TO_IHC1 L.2 host_naaeV ARCHAR50 03 h”t.ip/,4 host_modeld5 host jprotocol J 6 rea_i d
5、7 sn8 fixed-assetsV ARCHALINTINTINTV AKCKAKV AKCKAK50321251 S0 0.0 0 0 0 0网 KV LL KU LLUnn*eddevicsols MeidiSQL 9.1.0.4867J表model x|言数据|直词|文件 编招 辍索 工具 帮助1川3的今昌。卓5alv R国。动”翼A 八百命-I-以少;G限i主机:127 4 0.1|致据京device;B J U rsaJimd1 9/devices-1areas j hosts1 76.01 6 d64.0KiBKiBKxB_J基本索引外灌CMATE每添加键名 1字段 1关联表
6、1外原w/FK_niodel s_types/FK_ttiodel s_ven4ors份囹除、清除type.veiidmodel _ty typesmodl _ve.vendorx|protocols typ”vndwsi nformat i on_schea mysqlperorftance_schefta testie.o 1 6 0 ie.o1 60.0KiB l uB KiB KiB亨加:/逶加口肥、乳上向下#1名称 1数据类型1长度/设置 1无符号的1允许皿|填零|重认 注释_1 Bodel_id IHT2 model VAKCHAX,3 mod“_nndGr_id TNT*卜 4
7、model.type id INT 凶口叼问 5505 3AUTO JKCR.无号认值 无蝴、值 无默认信a!_VuiMed -.日心独。倒分5 vender s回 Iv覆主机1 27.0 0.1|救据宏:dvict 表vendors|:i数据|董海|,E,/U nnamed巳.devi ces 176 0 KiB_1基本迭项|索引外键|山分区1且CRE ATE代码|ALTE R代码|areas 1 6.0 KiBj添加筵名1字段 1关国 hosts 64.0 KaBO列除jr nodfel s 48.0 KiB_|protocol s 1 6.0 KxBA石际J typ.3 1 6 0 Ki
8、B田 information-schena 160 0 KiB申 电mysqlperf ora aiic js chea a宇及:。添加卿i、向上foTF由testt名称 1数据类空1长度/设置 1无符号的1允许皿|填零|默认|1vendord I NT10 V AU TO_IBC1.2vendor _co de V AKHAK50 0 03vondojnM。V MHA?.50 口画 04vendor-nejrie_cn V AP HA?.50 口画 0 1UnnMedVdevi types-Hei di SQL 9.1.0.4887文件 孀辑 越索 工具 帮助a .丁 J!?j 功 2|安中
9、U&M H 夕*,-;*-S W 噩%*|与一域学 9 O|叫主机:1 27Q0,l|数据库;d”ias 口表:t型5|而数据|哂7,基本 选项|索引-外键业分区IQCREA代码IgALTER代码|添加1 1键名 I字段 I关1。利除X号除宇他/添加删”同1_ 7向下*I名称 I数魁型|长度/设费 I无符号休I I允许皿|康霉|默认 一11 type IHT 3 M _ AUTO_IMC1.2 typjnsjcn VAKHAR 50 前 口 03 lypy VASCHAR 50 画 口 0F _J Unna pd:-devices1 76 0 KiB1 6.0 KiB64.0 KiB48 0
10、KiBIB 0 KiBereas hosts model s protocol sutypor ivendorsnform t i on_schea1 8 0 KiB1 60 0 KiBmysqlp ex-om anc jschee a testUanaaoddericesreas-Hei di SQL 9.1.0.4867文件 堀密 搜索 工具 泮助160 0B.CJ U nnwed-devices1 76 0 KiBE;r.areas1 1hosts64.0 KiBj model s48 0 KiBJ protocol s1 6 0 KiB typ”1 6 0 KiBj vendor$1
11、6 0 KiBKiBi nform t i on_sche a mysqlperforftance_scheaa test田 f+出口表I葛海据I查询I 口正I主机IZTU OJ 数据赤;d.vic”_墓本选项|索引外键I 分区1匕CRE ATE代码Is AU E R代码I越名 1字段 1关:州除X有除字段:添加.删生一回上7向下=1名称1科髅型I长度/设置 无符号的|允讣皿|填零|默认IHTV W fKAI503 无歌队俏 无期认值UnxiMicd 囹国由匕上向安;Oa_府 HI屿主机:1 27.0 0.1 I 匏据库 dtvic.x 表:protocol s|割数掘|宣询|,日“U nn
12、的d devices 1 76.0 KiB,,!weas 1 6.0 KiB!hosts 64.0 KiBmodel s 48.0 KiBTJSBSSHHBIMMi:1 typQ 二 1|vendors 1 6 Q KiBi nf or mat i on_scheaa 1 60.0 KiB_1基本选项|索引外健|大分区|;.CRE ATE代码|.ALTE R代码|IJ添加列除 X者眸键名 1钠 摩*室及:J漆加删1区、向上丁向下*I名称 I数触型 I长度/设量 I无符号的I允许皿I暇 I默认1 protocol_id IKT 2 回 口口 AUTO_ICK.2 protocol 7ARCHAR
13、 10 。四主机1 27.0 0.I I 数据M:dvc*S|J表;需 款据|卜 查洵|UnnM、国扇曲为软主机1 2ToeM|数据库:deve/表:tyj”其数据一:./U nnani i f图后“%12后 阖可;.13.心1主机127.0 0.1 I 敦据库;dvi/口袋:nndort 养数据查询I O二 R U rtnwiQddevices,vendors.。总伏约)cN devices176 0 KiB-sreas?-l hostsIE fl KiR、vendorjd|vendor_code vendor_name vendor_name_cn64.0 KiB MMMMI CS Cis
14、co 思科羲(中国)网络技术有限公司I model s48.0 KiB 2 3P Junpr 喇腑络|protocol s1 6.0 KiB 3 H3 H3C 华三通信技术相限公司=)typ”1 6.0 KiB 4 HW Huawe,结为技术布限公司:n vendorstS.GGB 5 RG RSe 福建显网银板网络有理公司E i nform t i on_sche a1 60.0 KiB 6 F5 F5 F5 Networks申 mysql7 AL Al l ot Al l。磋信公司|perforance_schea8 PL PadfetLogic Procera Network隹 test
15、9 AT Al bedTetess 支粉(AfcedTel ess)*懦殖搜索工七帮助/I L,-.,*回扇 M%;p er orsianc e_scheaa test|.J 1V nns.d/devices1 76 0 KiB|areas1 6 0 KiBKosts64.0 KiBj no del s48 0KiBprotocol s1 6 0KiBJ typ”1 6 0KiBj vendors1 6 0KiBi nform at i g_schMa mysql1 60 0KiB主机127QCU|救据庞;表再数据|董询|,.device.hoU:51总记录 收约)下一1砺所有|,排俘|hos
16、tjd|hostjame|hostjp-hostjnodeljd|host_pfOtoc&|areajd|snTFTP服务器准备:Cisco TFTP Serrer计就机不地磁盘(C J Tool s.Cixco TFTP Srvr 0(|Cisco TFTP Server文件(F)编辑3)查看 工具CD 帮助00组织 剑打开新建文件夹4收藏夹下载品桌面1最近访问的位置。快锹.屋频片档乐 祝图文音名称.Log4,Common,dl l tftp exeTFTPSei-v.CU TQ TFTPServ.hip区芭 tftpxvc dl l反化说明.txtI修改日期 I类黄 I大小2015/4/2
17、9 13:30文件夹2015/4/29 13:30应用程序扩展 KB2015/4/29 13:30应用程序 80 KB2015/4/29 13:30CNT文件 2 KB2015/4/29 13:30部助文件 33 KBTFTPStrvtr.x 2015/4/28 13:30 应用程序 23 J20 1 5/4/29 1 3:30 应用程序扩展 19 KB2015/4/29 13:30 文本文档 2 KB备份连接设备备份配置的代码:importsysimporttelnetlibimporttimeimportpymysqlimportosimportshutilimport loggingim
18、port paramikodef sqlconnect():cur_host=conn.cursor()sql_host=select host_name?host_ip?area_idhost_model_idjhost_protocol from hosts n_host=cur_host.execute(sql_host)for row_host in cur_host.fetchall():host_name=row_host0host_ip=row_host1host_area=row_host2host_model_id=row_host3host_protocol=row_hos
19、t4if host_protocol=1:backup_telnet(host_name?host_ip,host_area,host_model_id)elif host_protocol=2:backup_ssh(host_name,host_ip,host_area,host_model_id)def backup_telnet(b_name,b_ip,b_area,b_model):if 2=b_model=3 or b_model=5 or b_model=47:backup_telnet_h3_l(b_namej b_ip,b_area)elif b_model=4 or 7=b_
20、model=8:backup_telnet_h3_2(b_name,b_ip,b_area)elif 10=b_model=14 or b_model=17 or 48=b_model=50:backup_telnet_hw_l(b_namej b_ip,b_area)elif b_model=6 or 15=b_model=16:backup_telnet_hw_2(b_name,elif 19=b model=38 or 40b_ip,b_area)=b model=44 or b model=46:backup_telnet_rg_l(b_name,b_ip,b_area)elif b_
21、model=18:backup_telnet_rg_2(b_nameJ b_ip,b_area)elif b_model=39:backup_telnet_rg_3(b_name,b_ip,b_area)elif b_model=1:backup_telnet_jp_l(b_name,b_ip,b_area)elif b_model=9:backup_telnet_at_l(b_name,b_ip,b_area)else:logging.warning(b_name+(+b_ip+)暂不支持该设备!”)def backup_ssh(b_name,b_ip,b_area,b_model):if
22、2=b_model=3 or b_model=5 or b_model=47:logging.warning(b_name+(+b_ip+elif b_model=4 or 7=b_model=8:logging.warning(b_name+(+b_ip+elif 10=b_model=14 or b_model=17 logging.warning(b_name+(+b_ip+elif b_model=6 or 15=b_model=16:logging.warning(b_name+(+b_ip+elif 19=b_model=38 or 40=b_model logging.warni
23、ng(b_name+(+b_ip+)暂不支持该设备!)暂不支持该设备!)or 48=b_model=50:)暂不支持该设备!)暂不支持该设备!),timeout=2)tn.write(hostconfigure+bn)tn.read_until(b#,timeout=2)tn.write(hostshow_config+bn)time.sleep(6)read_show=tn.read_very_eager()if str(read_show).index(edit)0:read_file_temp=open(tftp_dir+config-tmp.txt,wb)read_file_temp.
24、write(read_show)read_file_temp.close()replace=b(more)read_file_input=open(tftp_dir+config-tmp.txt,rb)read_file_output=open(tftp_dir+config.txt,wb)for read_line in read_file_input:read_file_output.write(read_line.replace(replace,b).lstrip()read_file_input.close()os.remove(tftp_dir+config-tmp.txt)read
25、file_output.close()movefile_txt(name,ip,area)except TimeoutError:logging.warning(name+(+ip+)TimeoutError:WinError 10060由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。)except FileNotFoundError:logging,er nor(name+(+ip+)FileNotFoundError:未成功备份 show|display set!)except ConnectionResetError:logging,warning(name+(+
26、ip+)Connection Reset Error:WinError 10054远程主机强迫关闭了一个现有的连接。”)except ValueError:logging,warning(name+(+ip+)ValueError:substring not found 备份失败,可能是由于密码错误导致。”)except:logging.error(name+(+ip+)发生未知错误!”)def backup_ssh_jp_l(name,ip,area):host=hostname=name.encode(encoding=utf-8)hostip=ip.encode(encoding=utf
27、8)host user =b用户名 host password =b密码 hostcmd=bconfigure;show|display set try:ssh=paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()ssh.connect(hostip,22,hostuser,hostpassword,timeout=3)stdin,stdout,stderr=ssh.exec_command(hostcmd)read_show=stdout.read()ssh.close()if str(re
28、ad_show).index(host-name)0:read_file_temp=open(tftp_dir+config-tmp.txt,wb)read_file_temp.write(read_show)read_file_temp.close()replace=b(more)read_file_input=open(tftp_dir+config-tmp.txt,rb)read_file_output=open(tftp_dir+config.txt wb)for read_line in read_file_input:read_file_output.write(read_line
29、replace(replace,b).lstrip()read_file_input.close()os.remove(tftp_dir+config-tmp.txt)read_file_output.close()movef(name,ip,area)except TimeoutError:logging.warning(name+(+ip+)TimeoutError:WinError 10060由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。)except FileNotFoundError:logging.ernor(name+(+ip+except Connec
30、tionResetError:logging.warning(name+(+ip except ValueError:logging.warning(name+(+ip except:logging.ernor(name+(+ip+)FileNot Found Error:未成功备份 show+)ConnectionResetErnor:WinError+)ValueError:substring not found)发生未知错误!)|display set!)10054远程主机强迫关闭了一个现有的连接。”)备份失败,可能是由于密码错误导致。def backup_telnet_at_l(nam
31、ej ip,area):host=hostname=name.encode(encoding=utf-8)hostip=ip.encode(encoding=utf-8)host user =b用户名host password =b密码hostshow_config=bshow config dynamic try:tn=telnetlib.Telnet(hostip)tn.set_debuglevel(2)tn.read_until(blogin:,timeout=2)tn.write(hostuser+bn)tn.read_until(bPassword:,timeout=2)tn.wri
32、te(hostpassword+bn)tn.read_until(b,timeout=2)tn.write(hostshow_config+bn)time.sleep(6)read_show=tn.read_very_eager()if str(read_show).index()0:read_file_temp=open(tftp_dir+config-tmp.txt,wb)read_file_temp.write(read_show)read_file_temp.close()replace=b(more)read_file_input=open(tftp_dir+config-tmp.t
33、xt,rb)read_file_output=open(tftp_dir+config.txt,wb)for read_line in read_file_input:read_file_output.write(read_line.replace(replace_lj b).lstrip()read_file_input.close()os.remove(tftp_dir+config-tmp.txt)read_file_output.close()movefile_txt(name,ip,area)except TimeoutError:logging.warning(name+(+ip+
34、)TimeoutError:WinError 10060由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。)except FileNotFoundError:logging.ernor(name+(+except ConnectionResetError:logging.warning(name+(except ValueError:logging.warning(name+(except:logging.ernor(name+(+ip+)FileNotFoundError:未成功备份 show|display set!)10054远程主机强迫关闭了一个现有的连接。)备份失
35、败,可能是由于密码错误导致。+ip+)ConnectionResetErnor:WinError not found+ip ip+)ValueError:substring)发生未知错误!)def backup_telnet_h3_l(name ip,area):host=hostname=name.encode(encoding=utf-8)hostip=ip.encode(encoding=utf-8)hostfuser=b用户名host password =b密码host tftp =btftp put startup.cfg config.txt try:tn=telnetlib.Te
36、lnet(hostip)tn.set_debuglevel(2)tn.read_until(bUsername:,timeout=2)tn.write(hostuser+bn)tn.read_until(bPassword:,timeout=2)tn.write(hostpassword+bn)tn.read_until(b,timeout=2)tn.write(hosttftp+bn)tn.read_until(bsuccessfully,timeout=6)movefile_txt(name,ip,area)except TimeoutError:logging.warning(name+
37、ip+)TimeoutError:WinError 10060由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。)except FileNotFoundError:logging,er nor(name+(+ip+)FileNotFoundError:未成功备份 startup,cfg!)except ConnectionResetError:logging,warning(name+(+ip+)Connection Reset Error:WinError 10054远程主机强迫关闭了一个现有的连接。”)except:logging.error(name+(+ip+)
38、发生未知错误!”)def backup_telnet_h3_2(name?ip,area):host=hostname=name.encode(encoding=utf-8)hostip=ip.encode(encoding=utf-8)host user =b用户名 host password =b密码 host tftp =btftp put config.cfg config.txt try:tn=telnetlib.Telnet(hostip)tn.set_debuglevel(2)tn.read_until(bUsername:,timeout=2)tn.write(hostuser
39、bn)tn.read_until(bPassword:,timeout=2)tn.write(hostpassword+bn)tn.read_until(b,timeout=2)tn.write(hosttftp+bn)tn.read_until(bsuccessfully,timeout=6)movefile_txt(name,ip,area)except TimeoutError:logging.warning(name+(+ip+)TimeoutError:WinError 10060由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。)except FileNotF
40、oundError:logging,error(name+(+ip+)FileNotFoundError:未成功备份 config.cfg!)except ConnectionResetError:logging,warning(name+(+ip+)Connection Reset Er nor:WinError 10054远程主机强迫关闭了一个现有的连接。)except:logging.error(name+(+ip+)发生未知错误!”)def backup_telnet_hw_l(name?ip,area):host=hostname=name.encode(encoding=utf-8
41、)hostip=ip.encode(encoding=utf-8)host user =b用户名 host password =b密码 host tftp =btftp put vrpcfg.zip config.zip try:tn=telnetlib.Telnet(hostip)tn.set_debuglevel(2)tn.read_until(bUsername:,timeout=2)tn.write(hostuser+bn)tn.read_until(bPassword:,timeout=2)tn.write(hostpassword+bn)tn.read_until(b,timeou
42、t=2)tn.write(hosttftp+bn)tn.read_until(bsuccessfully,timeout=6)movefile_zip(name,ip,area)except TimeoutError:logging.warning(name+(+ip+)TimeoutError:WinError 10060由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。)except FileNotFoundError:logging,error(name+(+ip+)FileNotFoundError:未成功备份 config.txt!)except Connecti
43、onResetError:logging,warning(name+(+ip+)Connection Reset Error:WinError 10054远程主机强迫关闭了一个现有的连接。)except:logging,error(name+(+ip+)发生未知错误!”)def backup_telnet_hw_2(name?ip,area):host=hostname=name.encode(encoding=utf-8)hostip=ip.encode(encoding=utf-8)host user =b用户名 host password =b密码host tftp =btftp put
44、 vrpcfg.cfg config.txt try:tn=telnetlib.Telnet(hostip)tn.set_debuglevel(2)tn.read_until(bUsername:,timeout=2)tn.write(hostuser+bn)tn.read_until(bPassword:,timeout=2)tn.write(hostpassword+bn)tn.read_until(b,timeout=2)tn.write(hosttftp+bn)tn.read_until(bsuccessfully,timeout=6)movefile_txt(name,ip,area
45、)except TimeoutError:logging.warning(name+(+ip+)TimeoutError:WinError 10060由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。)except FileNotFoundError:logging,er nor(name+(+ip+)FileNotFoundError:未成功备份 vrpcfg.cfg!)except ConnectionResetError:logging,warning(name+(+ip+)Connection Reset Error:WinError 10054远程主机强迫关闭了一
46、个现有的连接。”)except:logging,error(name+(+ip+)发生未知错误!”)def backup_telnet_rg_l(name?ip,area):host=hostname=name.encode(encoding=utf-8)hostip=ip.encode(encoding=utf-8)host user =b用户名 host password =b密码 host tftp =bcopy flash:config.text tftp:/config.txt hostconfig=bconfigure terminal hostuser_config=bno us
47、ername heqijun hostexit=bexithostfwr=bwrtry:tn=telnetlib.Telnet(hostip)tn.set_debuglevel(2)tn.read_until(bUsername:,timeout=2)tn.write(hostuser+bn)tn.read_until(bPassword:,timeout=2)tn.write(hostpassword+bn)tn.read_until(b#,timeout=2)tn.write(hosttftp+bn)tn.read_until(blength,timeout=6)movefile_txt(
48、name,ip,area)except TimeoutError:logging.warning(name+(+ip+)TimeoutError:WinError 10060由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。)except FileNotFoundError:logging,er nor(name+(+ip+)FileNotFoundError:未成功备份 config.txt!)except ConnectionResetError:logging,warning(name+(+ip+)Connection Reset Error:WinError 100
49、54远程主机强迫关闭了一个现有的连接。”)except:logging.error(name+(+ip+)发生未知错误!”)def backup_telnet_rg_2(name?ip,area):host=hostname=name.encode(encoding=utf-8)hostip=ip.encode(encoding=utf-8)host user =b用户名 host password =b密码 host tftp =bcopy flash:ap-config.text tftp:/叩-config.txt try:tn=telnetlib.Telnet(hostip)tn.se
50、t_debuglevel(2)tn.read_until(bUsername:,timeout=2)tn.write(hostuser+bn)tn.read_until(bPassword:,timeout=2)tn.write(hostpassword+bn)tn.read_until(b#J timeout=2)tn.write(hosttftp+bn)tn.read_until(blength,timeout=6)except TimeoutError:logging.warning(name+(+ip+)TimeoutError:WinError 10060由于连接方在一段时间后没有正