日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2025/3/15 python 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python通过ssh配置交换机_配置(通过SSH)Cisco交换机的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'

總結

以上是生活随笔為你收集整理的python通过ssh配置交换机_配置(通过SSH)Cisco交换机的Python脚本的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。