配置(通过SSH)Cisco交换机的Python脚本

2024-10-01 09:41:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我的环境中有很多cisco交换机和路由器。我有三套凭证(其中只有一套适用于特定设备)。然后我在记事本上列出IP地址(子.txt). 以及另一个记事本上的配置(配置文件)在

目的是从配置文件通过SSH到IP列表列表。如果设备接受了命令,则应将日志放入成功.txt如果由于某种原因不接受该命令,则应附加一个日志失败.txt在

但这行不通。你能帮我修好吗?在

import paramiko
import time
import sys
import logging
import socket
import pexpect
import traceback
from pexpect.popen_spawn import PopenSpawn



remote_conn_pre = paramiko.SSHClient()
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ips = [i.strip() for i in open("sub.txt")] 
user_local = "user1"
pass_local = "pass1"
user_aspac = "user2"
pass_aspac = "pass2"
user_batcca = "user3"
pass_batcca = "pass3"

g = open('config.txt', 'r+')
str = g.read()
g.close

success = open('success.txt', 'a')
fail = open('failed.txt', 'a')

paramiko.util.log_to_file("paramiko.log")

for ip in ips:
    try:
        remote_conn_pre.connect(ip, username=user_local, password=pass_local, timeout=4, look_for_keys=False, allow_agent=False)
        #print ("SSH connection established to %s" + ip)
        remote_conn = remote_conn_pre.invoke_shell()
        print (ip + ' === local credential')
        #remote_conn.send("show switch\n")
        remote_conn.send((str))
        time.sleep(2) 
        output = remote_conn.recv(5000)
        print (output)

    except paramiko.AuthenticationException:
        try:
            remote_conn_pre.connect(ip, username=user_aspac, password=pass_aspac, timeout=4, look_for_keys=False, allow_agent=False)
            remote_conn1 = remote_conn_pre.invoke_shell()
            print ip + ' === Global Credentials'
            #output = remote_conn.recv(500)
            #remote_conn.send("show switch")
            #remote_conn.send("\n")
            remote_conn1.send((str))
            time.sleep(2) 
            output1 = remote_conn1.recv(5000)
            print (output1)
        except paramiko.AuthenticationException:
            try:
                #remote_conn_pre.connect(ip, username=user_batcca, password=pass_batcca, timeout=4, look_for_keys=False, allow_agent=False)
                #remote_conn2 = remote_conn_pre.invoke_shell()
                child = pexpect.popen_spawn.PopenSpawn('ssh ' + user_batcca + '@' + ip)
                child.expect ('[pP]assword:')
                child.sendline(pass_batcca)
                print ip + ' === BATCCA Credential'
                #output2 = remote_conn2.recv(5000)
                for line in open('Config.txt').xreadlines():
                    child.sendline(line)
                    i = child.expect (['#', '^'])
                    if i==0:
                        success.write(ip + '\t' + line +'\n')
                    elif i==1:
                        fail.write(ip + '\t' + line +'\n')
                    time.sleep(5)
                    output2 = child.recv(5000)
                    print (output2)

            except paramiko.AuthenticationException:
                print ip + ' === Bad credentials'
                remote_conn3 = remote_conn_pre.invoke_shell()
                output3 = remote_conn3.recv(5000)
                print (output3)
    except paramiko.SSHException:
        print ip + ' === Issues with ssh service'
    except socket.error:
        print ip + ' === Device unreachable'

Tags: importiptxtfalsechildparamikoforremote