批量备份网络设备配置

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

ftp 备份配置,save保存配置

paramiko,re

例子


ssh = paramiko.SSHClient()    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        ssh.connect(
            hostname=host,
            username=user,
            password=passwd,
            port=22,
            pkey=None, look_for_keys=False,
            timeout=3, allow_agent=False)
        remote_conn = ssh.invoke_shell()
        time.sleep(0.1) # 设置间隔


        for cmdline in cmdlist:
            remote_conn.send("%s\n" % cmdline)
            time.sleep(0.5)

        output = remote_conn.recv(1000).decode()
        devname = re.findall(r'\<(.*)\>.*',output)[0] #正则导入主机名
        remote_conn.send('rename startup.cfg %s-%s.cfg-%s\n' % (devname,host,date))
        time.sleep(1)


        remote_conn.send('save\n')  #保存设备当前配置
        time.sleep(0.5)
        save = remote_conn.recv(1000).decode()
        if save.find('written') > -1:
            remote_conn.send("Y\n")
            time.sleep(1)
           # print(remote_conn.recv(1000))
            remote_conn.send("\n")
            # print(remote_conn.recv(1000))
            remote_conn.send("Y\n")
            # time.sleep(5)
            # print(remote_conn.recv(1000))

转载于:https://my.oschina.net/attacker/blog/2396128

你可能感兴趣的:(批量备份网络设备配置)