最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)網(wǎng)絡(luò)自動化eNSP

 更新時間:2021年05月27日 09:33:23   作者:text1.txt  
這篇文章主要介紹了Python實(shí)現(xiàn)網(wǎng)絡(luò)自動化eNSP,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.使用Paramiko登陸到單臺交換機(jī)

實(shí)驗(yàn)拓?fù)?/h3>

云彩橋接到本機(jī)環(huán)回接口:192.168.1.1/24
三層交換機(jī)IP:192.168.1.2/24

在這里插入圖片描述

實(shí)驗(yàn)要求

使用Python Paramiko 模塊實(shí)現(xiàn)SSH 登錄單個交換機(jī)(192.168.56.2/24),配置LoopBack0地址:1.1.1.1/32。配置完成后,保存退出。

實(shí)驗(yàn)步驟 配置交換機(jī)管理地址,并測試與主機(jī)虛擬網(wǎng)卡連通性

[Huawei]vlan 10
[Huawei]int vlan 10
[Huawei-Vlanif10]ip add 192.168.1.2 24
[Huawei-GigabitEthernet0/0/1]port link-type access 
[Huawei-GigabitEthernet0/0/1]port default vlan 10

在這里插入圖片描述

在這里插入圖片描述

配置三層交換機(jī)開啟 SSH 服務(wù)端,配置 SSH 賬號密碼。

[Huawei]user-interface vty 0 4
[Huawei-ui-vty0-4]authentication-mode aaa
[Huawei-ui-vty0-4]protocol inbound ssh
[Huawei-aaa]local-user python password cipher 123
[Huawei-aaa]local-user python privilege level 3
[Huawei-aaa]local-user python service-type ssh 
[Huawei]stelnet server enable 
[Huawei]ssh authentication-type default password 

Python代碼

import paramiko
import time

ip = '192.168.56.2'
username = 'python'
password = '123'

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) //默認(rèn)情況下,Paramiko會拒絕任何未知的SSH public keys,使用此函數(shù)使其接收來自交換機(jī)提供的public keys。
ssh_client.connect(hostname=ip, username=username, password=password, look_for_keys=False)
print('Successfully connect to ' + ip)

commend = ssh_client.invoke_shell()
commend.send('sys\n')
commend.send('interface LoopBack 0\n')
commend.send('ip add 1.1.1.1 255.255.255.255\n')
commend.send('return\n')
commend.send('save\n')
commend.send('y\n')

time.sleep(3) //稍等3秒,然后執(zhí)行以下操作
output = commend.recv(65535) //截取本次運(yùn)行script后的所有輸出記錄,將其assign給output變量
print(output.decode("ascii"))

ssh_client.close()

查看運(yùn)行結(jié)果

在這里插入圖片描述

在交換機(jī)上查看

在這里插入圖片描述

也可以在交換機(jī)上debuggiing ip packet可以看到日志

2.使用Paramiko登陸到連續(xù)子網(wǎng)交換機(jī)

實(shí)驗(yàn)拓?fù)?/h3>

連續(xù)子網(wǎng)三層交換機(jī):管理地址 192.168.1.2/24 to 192.168.1.5/24

在這里插入圖片描述

實(shí)驗(yàn)要求

登陸到各臺交換機(jī),并為其配置vlan 11 to 15,保存配置并退出。

實(shí)驗(yàn)步驟

配置管理口IP地址,并配置SSH Server 登陸名以及密碼等

python代碼

import paramiko
import time

#import getpass
#username = input('Username: ')
#password = getpass.getpass('Password: ') //pycharm中該模塊運(yùn)行沒反應(yīng),用戶名和密碼還是直接寫的

username = 'python'
password = '123'

for i in range(2, 6):
    ip = '192.168.1.' + str(i)
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=ip, username=username, password=password, look_for_keys=False)
    command = ssh_client.invoke_shell()
    print('Successfully connect to ' + ip)
    command.send('sys\n')
    for j in range(11, 16):
        print('正在創(chuàng)建VLAN: ' + str(j))
        command.send('vlan ' + str(j) + '\n')
        time.sleep(1)
    command.send('return\n')
    command.send('save\n')
    command.send('y\n')
    time.sleep(2)
    output = command.recv(65535).decode('ascii')
    print(output)

ssh_client.close()

運(yùn)行結(jié)果

在這里插入圖片描述

在這里插入圖片描述

3.Paramiko登陸不連續(xù)子網(wǎng)交換機(jī)

實(shí)驗(yàn)拓?fù)?/h3>

將交換機(jī)LSW5的管理接口ip更改為192.168.1.6/24,使交換機(jī)ip不在同一網(wǎng)段

在這里插入圖片描述

實(shí)驗(yàn)要求

使用Paramiko登陸四臺ip不連續(xù)的交換機(jī),并給其配置vlan11 to 15

實(shí)驗(yàn)步驟

創(chuàng)建一個文本文檔,將需要配置的交換機(jī)的ip地址寫入,這里我在Desktop下創(chuàng)建了一個名為ip.txt文檔

在這里插入圖片描述

使用open函數(shù),打開文件,進(jìn)行操作,實(shí)現(xiàn)不連續(xù)子網(wǎng)調(diào)用

import paramiko
import time

username = 'python'
password = '123'

f = open('C:/Users/DELL/Desktop/ip.txt', 'r')
for line in f.readlines():
    ip = line.strip()
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=ip, username=username, password=password)
    print('Successfully connect to ', ip)
    command = ssh_client.invoke_shell()
    command.send('sys\n')
    command.send('vlan batch 11 to 15\n')
    time.sleep(2)
    command.send('return\n')
    command.send('save\n')
    command.send('y\n')
    time.sleep(2)
    output = command.recv(65535).decode('ascii')
    print(output)

f.close()
ssh_client.close()

查看運(yùn)行結(jié)果

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

4.sys.argv[ ] 實(shí)現(xiàn)靈活調(diào)用腳本所需文件

實(shí)驗(yàn)拓?fù)?/h3>

假設(shè)1.2和1.3為一組,1.4和1.6為一組

在這里插入圖片描述

實(shí)驗(yàn)要求

同時修改不同型號設(shè)備的配置,給SW1/3配置vlan11 to 15,SW4/5配置vlan16 to 20

實(shí)驗(yàn)步驟

創(chuàng)建兩個名為ip1.txt,command1.txt的文件,存儲1組的ip和要進(jìn)行的配置

在這里插入圖片描述

同樣創(chuàng)建兩個名為ip2.txt,command2.txt文件,存儲2組的ip和要進(jìn)行的配置

在這里插入圖片描述

在這里插入圖片描述

python代碼

import paramiko
import time
import sys

username = 'python'
password = '123'

ip_file = sys.argv[1]
cmd_file = sys.argv[2]

iplist = open(ip_file)
for line in iplist.readlines():
    ip = line.strip()
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=ip, username=username, password=password)
    print('Successfully connect to ', ip)
    command = ssh_client.invoke_shell()
    cmdlist = open(cmd_file, 'r')
    cmdlist.seek(0)
    for line in cmdlist.readlines():
        command.send(line + '\n')
        time.sleep(5)
    cmdlist.close()
    output = command.recv(65535)
    print(output)

iplist.close()
ssh_client.close()

查看運(yùn)行結(jié)果(pycharm不可以使用argv,在cmd里使用)

在這里插入圖片描述

5.SSH連接失敗處理

import paramiko
import time
import sys
import socket
import getpass

username = input('Username: ')
password = getpass.getpass('Password: ')
ip_file = sys.argv[1]
cmd_file = sys.argv[2]

switch_with_authentication_issue = []
switch_not_reachable = []

iplist = open(ip_file, 'r')
for line in iplist.readlines():
    try:
        ip = line.strip()
        ssh_client = paramiko.SSHClient()
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh_client.connect(hostname=ip, username=username, password=password,look_for_keys=False)
        print('Successfully connect to ' + ip)
        command = ssh_client.invoke_shell()
        cmdlist = open(cmd_file, 'r')
        cmdlist.seek(0)
        for cmd in cmdlist.readlines():
            command.send(cmd + '\n')
        time.sleep(1)
        cmdlist.close()
        output = command.recv(65535)
        print(output.decode("ascii"))
    except paramiko.ssh_exception.AuthenticationException:
        print('User authentication failed for ' + ip + '.')
        switch_with_authentication_issue.append(ip)
    except TimeoutError:
        switch_not_reachable.append(ip)

iplist.close()
ssh_client.close()

print('\nUser authentication failed for below switches: ')
for i in switch_with_authentication_issue:
    print(i)

print('\nBelow switchs are not reachable: ')
for i in  switch_not_reachable:
    print(i)

到此這篇關(guān)于Python實(shí)現(xiàn)網(wǎng)絡(luò)自動化eNSP的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python 網(wǎng)絡(luò)自動化 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • KMP算法精解及其Python版的代碼示例

    KMP算法精解及其Python版的代碼示例

    KMP算法基本上被人們用作字符串的匹配操作,這里我們就來介紹KMP算法精解及其Python版的代碼示例,需要的朋友可以參考下
    2016-06-06
  • python中sort()函數(shù)用法詳解

    python中sort()函數(shù)用法詳解

    Python和Java等編程語言一樣,提供了一種自動排序的方法,Java中是Arrays.sort()方法,而Python中是sort()方法,下面這篇文章主要給大家介紹了關(guān)于python中sort()函數(shù)用法的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • Python中TypeError: unhashable type: ‘list‘錯誤的解決方法

    Python中TypeError: unhashable type: ‘list‘錯誤的解

    在Python編程的領(lǐng)域中,數(shù)據(jù)類型的正確使用是確保程序正常運(yùn)行的關(guān)鍵要素之一,然而,開發(fā)者們常常會遇到一些由于數(shù)據(jù)類型使用不當(dāng)而引發(fā)的報錯,其中TypeError: unhashable type: 'list’就是一個比較典型的錯誤,那么,讓我們深入探究這個報錯問題,為大家提供全面的解決方案
    2024-10-10
  • Ubuntu18.04安裝 PyCharm并使用 Anaconda 管理的Python環(huán)境

    Ubuntu18.04安裝 PyCharm并使用 Anaconda 管理的Python環(huán)境

    這篇文章主要介紹了Ubuntu18.04安裝 PyCharm并使用 Anaconda 管理的Python環(huán)境的教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • 詳解Python中的Dict

    詳解Python中的Dict

    這篇文章主要為大家介紹了Python中的Dict,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • Python面向?qū)ο笾惡蛯ο髮?shí)例詳解

    Python面向?qū)ο笾惡蛯ο髮?shí)例詳解

    這篇文章主要介紹了Python面向?qū)ο笾惡蛯ο?結(jié)合實(shí)例形式詳細(xì)分析了Python面向?qū)ο笙嚓P(guān)的繼承、多態(tài)、類及對象等概念、原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-12-12
  • Python pysnmp使用方法及代碼實(shí)例

    Python pysnmp使用方法及代碼實(shí)例

    這篇文章主要介紹了Python pysnmp使用方法及代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • Python 實(shí)現(xiàn)二叉查找樹的示例代碼

    Python 實(shí)現(xiàn)二叉查找樹的示例代碼

    這篇文章主要介紹了Python 實(shí)現(xiàn)二叉查找樹的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • 使用Python實(shí)現(xiàn)在Excel工作表中創(chuàng)建、修改及刪除表格區(qū)域

    使用Python實(shí)現(xiàn)在Excel工作表中創(chuàng)建、修改及刪除表格區(qū)域

    在數(shù)據(jù)分析和自動化處理的工作中,Excel作為一種強(qiáng)大的工具被廣泛應(yīng)用,而通過Python來操作Excel工作表中的表格,可以極大提高工作效率,下面我們就來學(xué)學(xué)如何使用Python在Excel工作表中創(chuàng)建,修改及刪除表格吧
    2024-12-12
  • Python代碼解決RenderView窗口not found問題

    Python代碼解決RenderView窗口not found問題

    這篇文章主要介紹了Python代碼解決RenderView窗口not found問題,需要的朋友可以參考下
    2016-08-08

最新評論

旅游| 海伦市| 汶川县| 芜湖县| 安龙县| 饶阳县| 溆浦县| 大荔县| 澜沧| 翁牛特旗| 泸水县| 东乡族自治县| 东方市| 桃园县| 阿拉善盟| 滁州市| 二连浩特市| 孟连| 琼海市| 石首市| 长兴县| 长岭县| 博客| 武隆县| 云和县| 准格尔旗| 大竹县| 延吉市| 隆德县| 惠安县| 商洛市| 阿拉善左旗| 和顺县| 灵川县| 安康市| 眉山市| 永和县| 都匀市| 油尖旺区| 扎鲁特旗| 呼图壁县|