轉(zhuǎn)載請(qǐng)注明以下內(nèi)容:
來(lái)源:公眾號(hào)【網(wǎng)絡(luò)技術(shù)干貨圈】
作者:圈圈
ID:wljsghq
本文將詳細(xì)介紹如何利用Python腳本登錄到交換機(jī)并創(chuàng)建VLAN。
環(huán)境準(zhǔn)備
硬件與軟件要求
硬件要求:一臺(tái)支持SSH的網(wǎng)絡(luò)交換機(jī)
軟件要求:
Python 3.x
相關(guān)Python庫(kù):paramiko、netmiko
Python庫(kù)安裝
在開(kāi)始編寫腳本之前,需要安裝必要的Python庫(kù)。使用以下命令安裝:
pipinstallparamikonetmiko
了解交換機(jī)的基本操作
在登錄到交換機(jī)并創(chuàng)建VLAN之前,我們需要了解一些基本的交換機(jī)操作命令。這些命令通常通過(guò)SSH(Secure Shell)發(fā)送到交換機(jī)上執(zhí)行。以下是一些常見(jiàn)的交換機(jī)命令:
登錄交換機(jī):通過(guò)SSH使用用戶名和密碼登錄到交換機(jī)。
進(jìn)入全局配置模式:configure terminal
創(chuàng)建VLAN:vlan
命名VLAN:name
保存配置:write memory 或 copy running-config startup-config
使用Python腳本登錄交換機(jī)
使用Paramiko庫(kù)登錄交換機(jī)
paramiko是一個(gè)用于實(shí)現(xiàn)SSH協(xié)議的Python庫(kù),可以用來(lái)遠(yuǎn)程連接交換機(jī)。以下是一個(gè)簡(jiǎn)單的示例,展示如何使用paramiko登錄到交換機(jī):
importparamiko defssh_connect(hostname,username,password): #創(chuàng)建SSH客戶端對(duì)象 ssh=paramiko.SSHClient() #自動(dòng)添加主機(jī)密鑰 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #連接到交換機(jī) ssh.connect(hostname,username=username,password=password) returnssh #示例用法 hostname='192.168.1.1' username='admin' password='password' ssh=ssh_connect(hostname,username,password) print("成功登錄到交換機(jī)")
使用Netmiko庫(kù)登錄交換機(jī)
netmiko是基于paramiko封裝的一個(gè)庫(kù),專為網(wǎng)絡(luò)設(shè)備自動(dòng)化管理設(shè)計(jì),使用起來(lái)更為方便。以下是使用netmiko登錄到交換機(jī)的示例:
fromnetmikoimportConnectHandler defnetmiko_connect(hostname,username,password,device_type='cisco_ios'): #設(shè)備信息 device={ 'device_type':device_type, 'host':hostname, 'username':username, 'password':password, } #連接到交換機(jī) net_connect=ConnectHandler(**device) returnnet_connect #示例用法 hostname='192.168.1.1' username='admin' password='password' net_connect=netmiko_connect(hostname,username,password) print("成功登錄到交換機(jī)")
使用Python腳本創(chuàng)建VLAN
使用Paramiko創(chuàng)建VLAN
在成功登錄交換機(jī)后,可以使用paramiko發(fā)送命令創(chuàng)建VLAN。以下是一個(gè)完整的示例:
importparamiko importtime defssh_connect(hostname,username,password): ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname,username=username,password=password) returnssh defcreate_vlan(ssh,vlan_id,vlan_name): #打開(kāi)一個(gè)交互式Shell會(huì)話 remote_conn=ssh.invoke_shell() #進(jìn)入全局配置模式 remote_conn.send("configureterminal ") time.sleep(1) #創(chuàng)建VLAN remote_conn.send(f"vlan{vlan_id} ") time.sleep(1) #命名VLAN remote_conn.send(f"name{vlan_name} ") time.sleep(1) #退出配置模式 remote_conn.send("end ") time.sleep(1) #保存配置 remote_conn.send("writememory ") time.sleep(1) output=remote_conn.recv(65535).decode('utf-8') returnoutput #示例用法 hostname='192.168.1.1' username='admin' password='password' vlan_id=10 vlan_name='Test_VLAN' ssh=ssh_connect(hostname,username,password) output=create_vlan(ssh,vlan_id,vlan_name) print("VLAN創(chuàng)建成功") print(output)
使用Netmiko創(chuàng)建VLAN
使用netmiko庫(kù)創(chuàng)建VLAN的代碼更為簡(jiǎn)潔。以下是一個(gè)完整的示例:
fromnetmikoimportConnectHandler
defnetmiko_connect(hostname,username,password,device_type='cisco_ios'):
device={
'device_type':device_type,
'host':hostname,
'username':username,
'password':password,
}
net_connect=ConnectHandler(**device)
returnnet_connect
defcreate_vlan(net_connect,vlan_id,vlan_name):
commands=[
'configureterminal',
f'vlan{vlan_id}',
f'name{vlan_name}',
'end',
'writememory'
]
output=net_connect.send_config_set(commands)
returnoutput
#示例用法
hostname='192.168.1.1'
username='admin'
password='password'
vlan_id=10
vlan_name='Test_VLAN'
net_connect=netmiko_connect(hostname,username,password)
output=create_vlan(net_connect,vlan_id,vlan_name)
print("VLAN創(chuàng)建成功")
print(output)
腳本優(yōu)化與錯(cuò)誤處理
在實(shí)際應(yīng)用中,我們可能會(huì)遇到各種錯(cuò)誤和異常情況,例如登錄失敗、命令執(zhí)行失敗等。為了使腳本更加健壯,我們需要加入錯(cuò)誤處理機(jī)制。
使用Paramiko的錯(cuò)誤處理
以下是加入錯(cuò)誤處理后的paramiko腳本:
importparamiko
importtime
defssh_connect(hostname,username,password):
try:
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname,username=username,password=password)
returnssh
exceptparamiko.AuthenticationException:
print("認(rèn)證失敗,請(qǐng)檢查用戶名和密碼。")
exceptparamiko.SSHExceptionassshException:
print(f"無(wú)法建立SSH連接:{sshException}")
exceptExceptionase:
print(f"出現(xiàn)錯(cuò)誤:{e}")
defcreate_vlan(ssh,vlan_id,vlan_name):
try:
remote_conn=ssh.invoke_shell()
remote_conn.send("configureterminal
")
time.sleep(1)
remote_conn.send(f"vlan{vlan_id}
")
time.sleep(1)
remote_conn.send(f"name{vlan_name}
")
time.sleep(1)
remote_conn.send("end
")
time.sleep(1)
remote_conn.send("writememory
")
time.sleep(1)
output=remote_conn.recv(65535).decode('utf-8')
returnoutput
exceptExceptionase:
print(f"創(chuàng)建VLAN時(shí)出錯(cuò):{e}")
#示例用法
hostname='192.168.1.1'
username='admin'
password='password'
vlan_id=10
vlan_name='Test_VLAN'
ssh=ssh_connect(hostname,username,password)
ifssh:
output=create_vlan(ssh,vlan_id,vlan_name)
ifoutput:
print("VLAN創(chuàng)建成功")
print(output)
ssh.close()
使用Netmiko的錯(cuò)誤處理
以下是加入錯(cuò)誤處理后的netmiko腳本:
fromnetmikoimportConnectHandler,NetMikoAuthenticationException,NetMikoTimeoutException
defnetmiko_connect(hostname,username,password,device_type='cisco_ios'):
device={
'device_type':device_type,
'host':hostname,
'username':username,
'password':password,
}
try:
net_connect=ConnectHandler(**device)
returnnet_connect
exceptNetMikoAuthenticationException:
print("認(rèn)證失敗,請(qǐng)檢查用戶名和密碼。")
exceptNetMikoTimeoutException:
print("連接超時(shí),請(qǐng)檢查交換機(jī)的網(wǎng)絡(luò)連接。")
exceptExceptionase:
print(f"出現(xiàn)錯(cuò)誤:{e}")
defcreate_vlan(net_connect,vlan_id,vlan_name):
try:
commands=[
'configureterminal',
f'vlan{vlan_id}',
f'name{vlan_name}',
'end',
'writememory'
]
output=net_connect.send_config_set(commands)
returnoutput
exceptExceptionase:
print(f"創(chuàng)建VLAN時(shí)出錯(cuò):{e}")
#示例用法
hostname='192.168.1.1'
username='admin'
password='password'
vlan_id=10
vlan_name='Test_V
LAN'
net_connect=netmiko_connect(hostname,username,password)
ifnet_connect:
output=create_vlan(net_connect,vlan_id,vlan_name)
ifoutput:
print("VLAN創(chuàng)建成功")
print(output)
net_connect.disconnect()
-
交換機(jī)
+關(guān)注
關(guān)注
23文章
2922瀏覽量
104677 -
VLAN
+關(guān)注
關(guān)注
1文章
290瀏覽量
37912 -
python
+關(guān)注
關(guān)注
58文章
4881瀏覽量
90251 -
腳本
+關(guān)注
關(guān)注
1文章
411瀏覽量
29262
原文標(biāo)題:利用Python腳本怎么登錄到交換機(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)先 登錄
使用paramiko在eNSP的交換機(jī)中批量創(chuàng)建VLAN
ISM交換機(jī)如何添加VLAN呢?
[分享]常見(jiàn)網(wǎng)絡(luò)交換機(jī)故障及應(yīng)用問(wèn)答
華為路由器交換機(jī)VLAN配置實(shí)例
交換機(jī)VLAN是如何實(shí)現(xiàn)的
二層交換機(jī)web配置教程
聊聊科地網(wǎng)管PoE交換機(jī)的VLAN如何配置
交換機(jī)劃分vlan的原因是什么
登錄網(wǎng)絡(luò)交換機(jī)的三種方法
python巡檢華為交換機(jī)
使用Python腳本備份華為交換機(jī)的配置信息
利用Python腳本登錄到交換機(jī)并創(chuàng)建VLAN
評(píng)論