获取路由器current-config并且备份到数据库
import sqlite3
import paramiko
import re
import hashlib
import time
import os
#创建数据库
def createdb(dbname, tablename):
if os.path.exists(dbname):
os.remove(dbname)
conn = sqlite3.connect(dbname)
cursor = conn.cursor()
cursor.execute("create table %s(ip varchar (40),config varchar (9999),md5value varchar (9999))" %tablename)
conn.close()
#获取纯净的current-config(从sysname到return之间的配置)
def ssh_router(ip, username, passwd, port=22, cmd='dis cu'):
ssh1 = paramiko.SSHClient()
ssh1.load_system_host_keys()
ssh1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh1.connect(ip, port, username=username, password=passwd, timeout=2, compress=True)
except Exception:
print('无法连接到设备')
else:
shell = ssh1.invoke_shell()
shell.send(cmd + '\n')
time.sleep(2)
z = shell.rec