av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:105日期:2022-06-18 10:54:08
目錄1.使用Paramiko登陸到單臺(tái)交換機(jī)實(shí)驗(yàn)拓?fù)鋵?shí)驗(yàn)要求2.使用Paramiko登陸到連續(xù)子網(wǎng)交換機(jī)實(shí)驗(yàn)拓?fù)鋵?shí)驗(yàn)要求實(shí)驗(yàn)步驟3.Paramiko登陸不連續(xù)子網(wǎng)交換機(jī)實(shí)驗(yàn)拓?fù)鋵?shí)驗(yàn)要求4.sys.argv[ ] 實(shí)現(xiàn)靈活調(diào)用腳本所需文件實(shí)驗(yàn)拓?fù)鋵?shí)驗(yàn)要求實(shí)驗(yàn)步驟5.SSH連接失敗處理1.使用Paramiko登陸到單臺(tái)交換機(jī)實(shí)驗(yàn)拓?fù)?p>云彩橋接到本機(jī)環(huán)回接口:192.168.1.1/24三層交換機(jī)IP:192.168.1.2/24

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

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

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

實(shí)驗(yàn)步驟 配置交換機(jī)管理地址,并測(cè)試與主機(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

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

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

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

[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 paramikoimport timeip = ’192.168.56.2’username = ’python’password = ’123’ssh_client = paramiko.SSHClient()ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) //默認(rèn)情況下,Paramiko會(huì)拒絕任何未知的SSH public keys,使用此函數(shù)使其接收來(lái)自交換機(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(’sysn’)commend.send(’interface LoopBack 0n’)commend.send(’ip add 1.1.1.1 255.255.255.255n’)commend.send(’returnn’)commend.send(’saven’)commend.send(’yn’)time.sleep(3) //稍等3秒,然后執(zhí)行以下操作output = commend.recv(65535) //截取本次運(yùn)行script后的所有輸出記錄,將其assign給output變量print(output.decode('ascii'))ssh_client.close()

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

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

在交換機(jī)上查看

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

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

2.使用Paramiko登陸到連續(xù)子網(wǎng)交換機(jī)實(shí)驗(yàn)拓?fù)?p>連續(xù)子網(wǎng)三層交換機(jī):管理地址 192.168.1.2/24 to 192.168.1.5/24

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

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

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

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

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

python代碼

import paramikoimport time#import getpass#username = input(’Username: ’)#password = getpass.getpass(’Password: ’) //pycharm中該模塊運(yùn)行沒(méi)反應(yīng),用戶名和密碼還是直接寫(xiě)的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(’sysn’) for j in range(11, 16):print(’正在創(chuàng)建VLAN: ’ + str(j))command.send(’vlan ’ + str(j) + ’n’)time.sleep(1) command.send(’returnn’) command.send(’saven’) command.send(’yn’) time.sleep(2) output = command.recv(65535).decode(’ascii’) print(output)ssh_client.close()

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

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

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

3.Paramiko登陸不連續(xù)子網(wǎng)交換機(jī)實(shí)驗(yàn)拓?fù)?p>將交換機(jī)LSW5的管理接口ip更改為192.168.1.6/24,使交換機(jī)ip不在同一網(wǎng)段

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

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

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

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

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

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

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

import paramikoimport timeusername = ’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(’sysn’) command.send(’vlan batch 11 to 15n’) time.sleep(2) command.send(’returnn’) command.send(’saven’) command.send(’yn’) time.sleep(2) output = command.recv(65535).decode(’ascii’) print(output)f.close()ssh_client.close()

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

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

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

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

4.sys.argv[ ] 實(shí)現(xiàn)靈活調(diào)用腳本所需文件實(shí)驗(yàn)拓?fù)?p>假設(shè)1.2和1.3為一組,1.4和1.6為一組

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

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

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

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

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

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

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

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

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

python代碼

import paramikoimport timeimport sysusername = ’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里使用)

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

5.SSH連接失敗處理

import paramikoimport timeimport sysimport socketimport getpassusername = 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ò)自動(dòng)化eNSP的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python 網(wǎng)絡(luò)自動(dòng)化 內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产一卡二卡三卡 | 亚洲嫩草| 国产精品视频在线观看 | 美女毛片免费看 | 亚洲成人蜜桃 | 国产精品国产三级国产aⅴ无密码 | 久久久久国产精品一区三寸 | www.激情.com | 国产精品久久久久久 | 国产精品99久久久久久宅男 | 一区二视频| 国产一区二区三区欧美 | 男女视频免费 | 国产小网站 | 国产精品久久久久久久7电影 | 精品久久久久久久 | 一区二区视屏 | 美女天天干 | 免费人成在线观看网站 | 久草青青草 | 在线观看国产视频 | 国产最好的av国产大片 | 成人精品免费视频 | 超碰精品在线 | 久草新在线 | 成人国产精品久久 | 日本视频一区二区三区 | 中文字幕av一区二区三区 | 午夜精品一区二区三区在线观看 | 久久久久久久久久久爱 | 亚洲视频在线看 | www.久久| 野狼在线社区2017入口 | 国产夜恋视频在线观看 | 一二区成人影院电影网 | 中文字幕欧美在线观看 | 欧美不卡 | 色橹橹欧美在线观看视频高清 | 日韩一区二区三区精品 | 欧美一区2区三区3区公司 | 日韩在线不卡视频 |