來源:公眾號(hào)【網(wǎng)絡(luò)技術(shù)干貨圈】
作者:圈圈
ID:wljsghq
實(shí)驗(yàn)拓?fù)洌?/p>
cloud連接本機(jī),ip地址為192.168.56.1,五臺(tái)交換機(jī)的配置的地址為192.168.1.11~55。現(xiàn)在通過paramiko,ssh進(jìn)入五臺(tái)設(shè)備,并且在五臺(tái)設(shè)備上分別創(chuàng)建vlan10-vlan20這11個(gè)VLAN。
版本:python3.9
實(shí)驗(yàn)步驟:
一、ssh配置:
##創(chuàng)建秘鑰 [sw2]dsalocal-key-paircreate ##配置SSH認(rèn)證類型(密碼/其他) [sw2]sshuserprinauthentication-typepassword [sw2]sshuserprinservice-typestelnet [sw2]stelnetserverenable ##配置認(rèn)證模式 [sw2]user-interfacevty04 [sw2-ui-vty0-4]authentication-modeaaa//配置認(rèn)證模式 [sw2-ui-vty0-4]protocolinboundssh//允許ssh連接虛擬終端 ##配置本地用戶信息 [sw2]aaa [sw2-aaa]local-userprinpasswordcipherHuawei@123 [sw2-aaa]local-userprinprivilegelevel15 [sw2-aaa]local-userprinservice-typessh
二、python腳本:
importparamiko importtime importgetpass #使用input函數(shù),輸入SSH的用戶名 username=input('Username:') #通過getpass()函數(shù)接收密碼,密碼是不可見的,但是在windows上有bug,密碼可見 password=getpass.getpass('Password:') #創(chuàng)建一個(gè)列表,表示五臺(tái)設(shè)備最后8位的地址 ip_tail_list=[11,22,33,44,55] #使用for循環(huán),接受SSH的秘鑰,并分別依次連接到五臺(tái)設(shè)備,注意需要將i轉(zhuǎn)化為字符串 foriinip_tail_list: ip="192.168.56."+str(i) ssh_client=paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(hostname=ip,username=username,password=password) print("Successfullyconnectto",ip) #使用invoke_shell()喚醒shell界面 command=ssh_client.invoke_shell() #使用command.send()函數(shù)創(chuàng)建VLAN,并且設(shè)置每個(gè)VLAN的描述;未來保證設(shè)備能夠正常接受配置,每次創(chuàng)建1個(gè)VLAN后休息1s command.send("system ") forninrange(10,21): print("CreatingVlan"+str(n)) command.send("vlan"+str(n)+" ") command.send("descriptionPythonVlan"+str(n)+" ") time.sleep(1) #保存配置,并且通過command.recv()函數(shù)得到回信的信息,最多接受65535個(gè)字符 command.send("return ") command.send("save "+"y "+" ") time.sleep(2) output=command.recv(65535) print(output.decode('ascii')) #關(guān)閉連接 ssh_client.close()
如果管理的設(shè)備數(shù)目過多,可以直接通過讀取txt文件的方式獲取IP地址,僅需要將如下代碼:
#創(chuàng)建一個(gè)列表,表示五臺(tái)設(shè)備最后8位的地址 ip_tail_list=[11,22,33,44,55] #使用for循環(huán),接受SSH的秘鑰,并分別依次連接到五臺(tái)設(shè)備,注意需要將i轉(zhuǎn)化為字符串 foriinip_tail_list: ip="192.168.56."+str(i) ssh_client=paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(hostname=ip,username=username,password=password) #......省略中間部分 ssh_client.close()
更換為下述即可:
#使用open()函數(shù)打開ip_list文件,并將讀取的結(jié)果賦予f f=open("ip_list.txt","r") #調(diào)用readlines()函數(shù),返回IP地址的列表,并使用for循環(huán)遍歷;注意使用readlines()的每一個(gè)ip地址后帶有 ,需要通過strip()函數(shù)去除 foriinf.readlines(): ip=i.strip() ssh_client=paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(hostname=ip,username=username,password=password) #.......省略中間部分,在完成文件操作后,關(guān)閉文件 f.close() ssh_client.close()
執(zhí)行效果:
…
在設(shè)備上檢查是否配置成功,以SW1為例:
可以看到創(chuàng)建VLAN和添加VLAN描述成功。
-
交換機(jī)
+關(guān)注
關(guān)注
21文章
2659瀏覽量
100209 -
VLAN
+關(guān)注
關(guān)注
1文章
279瀏覽量
35786 -
網(wǎng)絡(luò)技術(shù)
+關(guān)注
關(guān)注
1文章
294瀏覽量
29518 -
python
+關(guān)注
關(guān)注
56文章
4809瀏覽量
85070 -
腳本
+關(guān)注
關(guān)注
1文章
392瀏覽量
14957
原文標(biāo)題:使用paramiko在eNSP的交換機(jī)中批量創(chuàng)建VLAN
文章出處:【微信號(hào):網(wǎng)絡(luò)技術(shù)干貨圈,微信公眾號(hào):網(wǎng)絡(luò)技術(shù)干貨圈】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
ISM交換機(jī)如何添加VLAN呢?
[分享]常見網(wǎng)絡(luò)交換機(jī)故障及應(yīng)用問答
華為路由器交換機(jī)VLAN配置實(shí)例
交換機(jī)VLAN是如何實(shí)現(xiàn)的
交換機(jī)路由VLAN配置視頻教程
中、小局域網(wǎng)交換機(jī)VLAN的網(wǎng)絡(luò)配置
聊聊科地網(wǎng)管PoE交換機(jī)的VLAN如何配置
交換機(jī)劃分vlan的原因是什么
配置不同VLAN之間通訊-使用三層交換機(jī)
![配置不同<b class='flag-5'>VLAN</b>之間通訊-使用三層<b class='flag-5'>交換機(jī)</b>](https://file1.elecfans.com/web2/M00/89/22/wKgaomR5o0eAYuUWAAEk62D47Uw983.png)
評(píng)論