欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

1.使用Paramiko登陸到單臺交換機

實驗拓撲

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

在這里插入圖片描述

實驗要求

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

實驗步驟 配置交換機管理地址,并測試與主機虛擬網(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

在這里插入圖片描述

在這里插入圖片描述

配置三層交換機開啟 SSH 服務端,配置 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()) //默認情況下,Paramiko會拒絕任何未知的SSH public keys,使用此函數(shù)使其接收來自交換機提供的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) //截取本次運行script后的所有輸出記錄,將其assign給output變量
print(output.decode("ascii"))

ssh_client.close()

查看運行結(jié)果

在這里插入圖片描述

在交換機上查看

在這里插入圖片描述

也可以在交換機上debuggiing ip packet可以看到日志

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

實驗拓撲

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

在這里插入圖片描述

實驗要求

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

實驗步驟

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

python代碼

import paramiko
import time

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

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()

運行結(jié)果

在這里插入圖片描述

在這里插入圖片描述

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

實驗拓撲

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

在這里插入圖片描述

實驗要求

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

實驗步驟

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

在這里插入圖片描述

使用open函數(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()

查看運行結(jié)果

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

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

實驗拓撲

假設1.2和1.3為一組,1.4和1.6為一組

在這里插入圖片描述

實驗要求

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

實驗步驟

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

在這里插入圖片描述

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

在這里插入圖片描述

在這里插入圖片描述

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()

查看運行結(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實現(xiàn)網(wǎng)絡自動化eNSP的實現(xiàn)的文章就介紹到這了,更多相關(guān)Python 網(wǎng)絡自動化 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python繪圖demo實現(xiàn)流程介紹

    python繪圖demo實現(xiàn)流程介紹

    這篇文章主要介紹了python繪圖demo實現(xiàn)流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-11-11
  • 基于numpy中數(shù)組元素的切片復制方法

    基于numpy中數(shù)組元素的切片復制方法

    今天小編就為大家分享一篇基于numpy中數(shù)組元素的切片復制方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • Python的Django框架中的表單處理示例

    Python的Django框架中的表單處理示例

    這篇文章主要介紹了Python的Django框架中的表單處理示例,表單處理是Django中的基礎(chǔ)操作,需要的朋友可以參考下
    2015-07-07
  • Python jieba庫分詞模式實例用法

    Python jieba庫分詞模式實例用法

    在本篇文章里小編給大家分享的是一篇關(guān)于Python jieba庫分詞模式實例用法,有興趣的朋友們可以學習參考下。
    2021-01-01
  • python中的字典操作及字典函數(shù)

    python中的字典操作及字典函數(shù)

    本篇文章給大家介紹了python中的字典,包括字典的操作,字典函數(shù)實現(xiàn)代碼,需要的朋友參考下吧
    2018-01-01
  • Python web開發(fā)之用Tornado框架制作簡易表白墻網(wǎng)站

    Python web開發(fā)之用Tornado框架制作簡易表白墻網(wǎng)站

    這篇文章將用Python做Web開發(fā)。在Python當中,WEB開發(fā)框架主要有三個,本文將利用Tornado框架做一個簡單的表白墻網(wǎng)站,感興趣的可以了解一下
    2022-02-02
  • Python 中@property的用法詳解

    Python 中@property的用法詳解

    這篇文章主要介紹了Python 中@property的用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • 用python讀取xlsx文件

    用python讀取xlsx文件

    這篇文章主要介紹了用python讀取xlsx文件的方法,幫助大家更好的利用python處理excel文件,感興趣的朋友可以了解下
    2020-12-12
  • matplotlib quiver箭圖繪制案例

    matplotlib quiver箭圖繪制案例

    這篇文章主要介紹了matplotlib quiver箭圖繪制案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python格式化輸出字符串的五種方法總結(jié)

    Python格式化輸出字符串的五種方法總結(jié)

    Python語言有許多優(yōu)點,常用于不同的領(lǐng)域,如數(shù)據(jù)科學、web開發(fā)、自動化運維等。本文將學習如何使用字符串中內(nèi)置的方法來格式化字符串,感興趣的可以了解一下
    2022-06-06

最新評論