來源:公眾號【網(wǎng)絡(luò)技術(shù)干貨圈】
作者:圈圈
ID:wljsghq
實驗拓撲:

cloud連接本機,ip地址為192.168.56.1,五臺交換機的配置的地址為192.168.1.11~55?,F(xiàn)在通過paramiko,ssh進入五臺設(shè)備,并且在五臺設(shè)備上分別創(chuàng)建vlan10-vlan20這11個VLAN。
版本:python3.9
實驗步驟:
一、ssh配置:
##創(chuàng)建秘鑰 [sw2]dsalocal-key-paircreate ##配置SSH認證類型(密碼/其他) [sw2]sshuserprinauthentication-typepassword [sw2]sshuserprinservice-typestelnet [sw2]stelnetserverenable ##配置認證模式 [sw2]user-interfacevty04 [sw2-ui-vty0-4]authentication-modeaaa//配置認證模式 [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)建一個列表,表示五臺設(shè)備最后8位的地址
ip_tail_list=[11,22,33,44,55]
#使用for循環(huán),接受SSH的秘鑰,并分別依次連接到五臺設(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è)置每個VLAN的描述;未來保證設(shè)備能夠正常接受配置,每次創(chuàng)建1個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個字符
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)建一個列表,表示五臺設(shè)備最后8位的地址 ip_tail_list=[11,22,33,44,55] #使用for循環(huán),接受SSH的秘鑰,并分別依次連接到五臺設(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()的每一個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描述成功。
-
交換機
+關(guān)注
關(guān)注
23文章
2866瀏覽量
103880 -
VLAN
+關(guān)注
關(guān)注
1文章
288瀏覽量
37499 -
網(wǎng)絡(luò)技術(shù)
+關(guān)注
關(guān)注
1文章
296瀏覽量
31033 -
python
+關(guān)注
關(guān)注
57文章
4856瀏覽量
89526 -
腳本
+關(guān)注
關(guān)注
1文章
407瀏覽量
29046
原文標題:使用paramiko在eNSP的交換機中批量創(chuàng)建VLAN
文章出處:【微信號:網(wǎng)絡(luò)技術(shù)干貨圈,微信公眾號:網(wǎng)絡(luò)技術(shù)干貨圈】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄

使用paramiko在eNSP的交換機中批量創(chuàng)建VLAN
評論