您好,登錄后才能下訂單哦!
這篇文章主要介紹了Python如何實現網絡自動化eNSP,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
云彩橋接到本機環回接口:192.168.1.1/24
三層交換機IP:192.168.1.2/24
使用Python Paramiko 模塊實現SSH 登錄單個交換機(192.168.56.2/24),配置LoopBack0地址:1.1.1.1/32。配置完成后,保存退出。
實驗步驟 配置交換機管理地址,并測試與主機虛擬網卡連通性
[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,使用此函數使其接收來自交換機提供的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秒,然后執行以下操作 output = commend.recv(65535) //截取本次運行script后的所有輸出記錄,將其assign給output變量 print(output.decode("ascii")) ssh_client.close()
查看運行結果
在交換機上查看
也可以在交換機上debuggiing ip packet可以看到日志
連續子網三層交換機:管理地址 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('正在創建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()
運行結果
將交換機LSW5的管理接口ip更改為192.168.1.6/24,使交換機ip不在同一網段
使用Paramiko登陸四臺ip不連續的交換機,并給其配置vlan11 to 15
實驗步驟
創建一個文本文檔,將需要配置的交換機的ip地址寫入,這里我在Desktop下創建了一個名為ip.txt文檔
使用open函數,打開文件,進行操作,實現不連續子網調用
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()
查看運行結果
假設1.2和1.3為一組,1.4和1.6為一組
同時修改不同型號設備的配置,給SW1/3配置vlan11 to 15,SW4/5配置vlan16 to 20
創建兩個名為ip1.txt,command1.txt的文件,存儲1組的ip和要進行的配置
同樣創建兩個名為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()
查看運行結果(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)
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Python如何實現網絡自動化eNSP”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。