python3 paramiko 巡檢網絡設備

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

image.png

image.png

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

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

terminal length 0
show run

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

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

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

#!/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公鑰改變的就連不上了,每個廠家都要重寫。。。。。

#!/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.。。。。

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

image.png

解決辦法 ,清理下公鑰

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

image.png

這邊for循環打幾個空格進去,匹配到--More--就敲起來,可以解決,就是靈活性太低,還是用全頁顯示比較好


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章