python3 paramiko 巡檢網絡設備

用paramiko作網絡設備巡檢,發現大坑,就是show run這種看配置有多頁存在的無法顯示第二頁,沒找到paramiko翻頁的地方,添加多個空格也不是很好使。python

image.png

image.png

避開這個坑,自動登入搞定了後面命令怎麼傳都是小事了,傳參參考第二個腳本吧。shell

cisco的全頁打印顯示配置信息的命令:

terminal length 0
show run

華爲和H3C的全頁打印顯示配置信息的命令:

user-interface vty 0 4
screen-length 0
display current-configuration

直接在命令裏面傳入全局模式密碼。bash

#!/usr/bin/python3
# -*- coding:utf-8 -*-


import paramiko
import time


def main(host, username, password, commands):
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(host, username=username, password=password,
                   port=22, allow_agent=False, look_for_keys=False)
    channel = client.invoke_shell()  # 請求交互式Shell會話
    for command in commands:
        channel.send(command + "\n")  # 發送指令
        while not channel.recv_ready():  # 等待數據到達
            time.sleep(1)
        output = channel.recv(40960)  # 從通道接收數據 nbytes(int)–讀取的最大字節數
        print(output.decode())
    client.close()


if __name__ == '__main__':
    host = '192.168.208.131'
    username = 'root'
    password = 'root.123'
    commands = ['enable', 'cisco', 'terminal length 0','show run', 'show ip int br', 'exit']  #全頁打印terminal length 0
    main(host, username, password, commands)
commands = ['enable', 'cisco', ,'show run', ' ',' ', 'exit']

多添加幾個空格仍是沒有解決翻頁的問題網絡

image.png

最先用pexpect寫的,能夠避開這個坑,就是靈活性過低。遇到ssh公鑰改變的就連不上了,每一個廠家都要重寫。。。。。ssh

#!/usr/bin/python
#-*- coding:utf-8 -*-


import pexpect
import sys
import time


def main(host,username,password,enable,commands):
    # host = '192.168.208.131'
    # username = 'root'
    # password = 'root.123'
    # enable = 'cisco'
    # commands = [ show processes memory ]
    commands = str(commands).split(';')
    child = pexpect.spawnu('ssh %s@%s' % (username,host))
    child.logfile = sys.stdout
    login = child.expect(['yes/no', '[P|p]assword:', pexpect.EOF, pexpect.TIMEOUT])
    if login == 0:
        child.sendline('yes')
        child.expect('[P|p]assword:')
        child.sendline('%s' % password)
    elif login == 1:
        child.sendline('%s' % password)
    child.expect('\>')
    child.sendline('enable')
    child.expect('[P|p]assword:')
    child.sendline('%s' % enable)
    for command in commands:
        child.expect('\#')
        child.sendline('%s' % command)
        index = child.expect(['--More--','\#'])
        if index == 0:
            for i in range(5):
                child.send(' ')
                time.sleep(1)
            #child.sendline(' ')
        child.sendline('')
        #time.sleep(2)
    child.expect('\#')
    child.sendline('exit')
    child.close()


if __name__ == '__main__':
    host = sys.argv[1]
    username = sys.argv[2]
    password = sys.argv[3]
    enable = sys.argv[4]
    commands = sys.argv[5]
    main(host,username,password,enable,commands)

測試用的是eve和GNS3 ,網絡模擬器我玩的賊6.。。。。ide

pexpect 又開始踩坑,模擬器每次開起來公鑰就變了,就登不上了,生產環境極少機率會這樣舊設備替換啥的,想一想仍是放棄這個,後面不知道會遇到多少坑。測試

image.png

解決辦法 ,清理下公鑰spa

[root@localhost ~]# echo > .ssh/known_hosts

image.png

這邊for循環打幾個空格進去,匹配到--More--就敲起來,能夠解決,就是靈活性過低,仍是用全頁顯示比較好3d

相關文章
相關標籤/搜索