溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用netmiko模塊連接H3C設(shè)備(閉坑——H3C設(shè)備的分

發(fā)布時(shí)間:2020-07-02 17:27:38 來源:網(wǎng)絡(luò) 閱讀:2357 作者:chier11 欄目:網(wǎng)絡(luò)管理

最近一直在測試Python中通過ssh協(xié)議連接huawei、h4c等網(wǎng)絡(luò)設(shè)備的paramiko和netmiko模塊。為什么選用這兩個(gè)自己去網(wǎng)上找答案。
有關(guān)paramiko模塊連接網(wǎng)絡(luò)設(shè)備的例子和遇到的問題,煩請(qǐng)參考:https://blog.51cto.com/chier11/2116155
本文只介紹使用netmiko模塊連接H3C網(wǎng)絡(luò)設(shè)備,并成功閉坑:h4c分屏顯示的問題。
發(fā)發(fā)牢騷:netmiko連接huawei設(shè)備的時(shí)候,在netmiko模塊中會(huì)自動(dòng)去掉分屏顯示功能,意思就是說所有大量信息全部顯示出來,舉例:display interface brief,查看400多端口的信息,系統(tǒng)會(huì)分屏顯示,但是在netmiko的huawei模塊中默認(rèn)設(shè)置了取消分屏顯示的指令,如下:screen-length 0 temporary(華為取消分屏顯示的命令)


from __future__ import print_function
from __future__ import unicode_literals
import time
import re
from netmiko.cisco_base_connection import CiscoBaseConnection
from netmiko.ssh_exception import NetMikoAuthenticationException
from netmiko import log

class HuaweiBase(CiscoBaseConnection):
    def session_preparation(self):
        """Prepare the session after the connection has been established."""
        self._test_channel_read()
        self.set_base_prompt()
        self.disable_paging(command="screen-length 0 temporary")
        # Clear the read buffer
        time.sleep(0.3 * self.global_delay_factor)
        self.clear_buffer()

    def config_mode(self, config_command="system-view"):
        """Enter configuration mode."""
return super(HuaweiBase, self).config_mode(config_command=config_command)

這里大概猜測了下,為什么在netmiko模塊在huawei的連接中有取消分屏顯示功能的命令,而H3C設(shè)備沒有呢,經(jīng)過和廠家溝通發(fā)現(xiàn),華為設(shè)備的取消分屏顯示功能可以針對(duì)用戶設(shè)置,而且是臨時(shí)。舉個(gè)例子:一個(gè)user用戶登錄華為設(shè)備后執(zhí)行取消分屏顯示命令后對(duì)設(shè)備中其他用戶沒影響(其他用戶還是分屏顯示),當(dāng)user用戶退出ssh的時(shí)候該用戶的分屏顯示功能會(huì)自動(dòng)恢復(fù),就像定義了:進(jìn)入自己家目錄的環(huán)境一樣方便強(qiáng)大。而H3C網(wǎng)絡(luò)設(shè)備的分屏顯示針對(duì)的是全局設(shè)備,而且并不是進(jìn)入、退出環(huán)境那樣臨時(shí)性生效一樣。那么如何解決這種坑呢,經(jīng)過多次網(wǎng)上查找和多次測試,終于完美實(shí)現(xiàn)一次性顯示完整的輸出大量信息。
if "---- More ----" in outp:
outp += conn1.send_command_timing(
' \n', strip_prompt=False, strip_command=False, normalize=False
) ###遇到more,就多輸入幾次個(gè)空格,normalize=False表示不取消命令前后空格。
代碼如下,


from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoTimeoutException
from netmiko.ssh_exception import NetMikoAuthenticationException

def Get_CRC():
    try:
        pynet1 = {
        'device_type': "hp_comware",
        'ip': "10.10.10.10",
        'username': "CTyunuser",
        'password': "P@ssw0rd6900",
        }
        conn1 = ConnectHandler(**pynet1)
        cmd='display counters inbound interface '
        #cmd = 'display interface brief \n'
        outp=conn1.send_command_timing(cmd)
        if "---- More ----" in outp:
            outp += conn1.send_command_timing(
                '            \n', strip_prompt=False, strip_command=False, normalize=False
            )      *###遇到more,就多輸入幾次個(gè)空格,normalize=False表示不取消命令前后空格*。
        outp1 = outp.split("\n")
        print (outp1)

    except (EOFError,NetMikoTimeoutException):
        print('Can not connect to Device')
    except (EOFError, NetMikoAuthenticationException):
        print('username/password wrong!')
    except (ValueError, NetMikoAuthenticationException):
        print('enable password wrong!')

if __name__=="__main__":
     Get_CRC()

以上是在實(shí)際中對(duì)華為和華三廠家的命令使用中的區(qū)別總結(jié)。如有不妥之處煩請(qǐng)大家指正。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI