python自动化控制设备有限公司_华为 Python网络自动化
哈嘍,大家好!我是藝博東 ,是一個思科出身、專注于華為的網工;好了,話不多說,我們直接進入正題。
光棍二十年,不知道情人節是什么鬼東西。還是好好學技術吧!努力、奮斗吧!為了早日走向人生巔峰,迎娶白富美!拼了
1、安裝環境并導入相關模塊
首先是安裝好Python3環境,接著安裝Paramiko模塊,然后輸入pip3 install paramiko。
pip3 install paramiko
更新pip
pip install --upgrade pip
OK
進入python,導入 paramiko模塊
import paramiko
2、創建VLAN并配置IP地址和路由協議
2.1 拓撲
2.2 簡單配置與測試
SW1
[Huawei]sysname SW1
[SW1]vlan 100
[SW1-vlan100]q
[SW1]int Vlanif 100
[SW1-Vlanif100]ip address 192.168.117.254 24
[SW1-Vlanif100]int g0/0/1
[SW1-GigabitEthernet0/0/1]p l a
[SW1-GigabitEthernet0/0/1]p d v 100
[SW1-GigabitEthernet0/0/1]q
[SW1]user-interface vty 0 4
[SW1-ui-vty0-4]authentication-mode aaa
[SW1-ui-vty0-4]protocol inbound ssh
[SW1-ui-vty0-4]q
[SW1]aaa
[SW1-aaa]local-user ybd password cipher 1008611
[SW1-aaa]local-user ybd privilege level 15
[SW1-aaa]local-user ybd service-type ssh
[SW1-aaa]q
[SW1]ssh user ybd authentication-type password
[SW1]ssh user ybd service-type stelnet
[SW1]stelnet server enable
eNSP路由器AR1的PING測試物理機
[SW1]ping 192.168.117.1
物理機PING測試eNSP路由器AR1
注意:做橋記得關閉防火墻,否則eNSP PING測不通物理機。
2.3 python腳本
import paramiko
import time
ip = "192.168.117.254" #交換機的IP地址
user = "ybd" #SSH的用戶名
pw = "1008611" #SSH的密碼
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ip, username=user , password=pw)
print("已成功登錄到eNSP上的交換機了!" , ip)
#連接成功后,調用invoke_shell()方法來喚醒shell,也就是華為系統命令行,同時把它賦值給command,方便后續調用。
command = ssh.invoke_shell()
#向設備發送命令,需要執行的命令。
command.send("system \n")
command.send("vlan 120 \n")
command.send("quit \n")
command.send("int vlan 120 \n")
command.send("ip add 192.168.120.1 24 \n")
command.send("quit \n")
command.send("vlan 130\n") #創建vlan 130
command.send("quit \n") #返回上一級
command.send("int vlan 130 \n") #進入vlan 130 視圖
command.send("ip add 192.168.130.1 24 \n") #配置IP地址
command.send("quit \n")
command.send("vlan 140\n")
command.send("quit \n")
command.send("int vlan 140 \n")
command.send("ip add 192.168.140.1 24 \n")
command.send("quit \n")
command.send("ospf 1 router-id 1.1.1.1 \n")
command.send("a 0 \n")
command.send("net 192.168.0.0 0.0.255.255 \n")
command.send("quit \n")
command.send("quit \n")
command.send("ip route-static 192.168.117.1 32 NULL 0 \n")
command.send("ospf 1 \n")
command.send("import-route static \n")
#使用sleep函數,讓腳步執行后休息2s,再回顯內容。65535是回顯多少個字符
time.sleep(3)
output = command.recv(65535)
print(output.decode("ascii"))
ssh.close() #配置完后,用close方法退出ssh
運行結果:
[SW1]display ip int brief
已OK
3、telnet 遠程登錄管理設備
3.1 拓撲
3.2 簡單配置與測試
AR1
[Huawei]sysname AR1
[AR1]int g0/0/0
[AR1-GigabitEthernet0/0/0]ip address 192.168.150.254 24
[AR1-GigabitEthernet0/0/0]q
[AR1]user-interface vty 0 4
[AR1-ui-vty0-4]authentication-mode password
Please configure the login password (maximum length 16):1008611
[AR1-ui-vty0-4]protocol inbound telnet
[AR1-ui-vty0-4]user privilege level 15
[AR1-ui-vty0-4]q
eNSP路由器AR1的PING測試物理機
[AR1]ping 192.168.150.1
物理機PING測試eNSP路由器AR1
3.3 python腳本
import telnetlib
import time
host ='192.168.150.254'
password='1008611'
_UserTag = '>'
_SysTag = ']'
tn = telnetlib.Telnet(host)
tn.read_until(b'Password:')
tn.write(password.encode('ascii') + b"\n")
UserTag = tn.read_until(_UserTag.encode('ascii'))
response = UserTag
print(response.decode('ascii'))
time.sleep(2)
tn.write(b"dir\n")
response = UserTag
if b'>' in response:
print(response.decode('ascii'))
time.sleep(2)
tn.write(b"system-view\n")
SysTag = tn.read_until(_SysTag.encode('ascii'))
response = SysTag
print(response.decode('ascii'))
time.sleep(2)
tn.close()
print("close")
運行結果:
困難越大,榮耀也越大。——西塞羅
好了這期就到這里了,如果你喜歡這篇文章的話,請點贊評論分享收藏,如果你還能點擊關注,那真的是對我最大的鼓勵。謝謝大家,下期見!
總結
以上是生活随笔為你收集整理的python自动化控制设备有限公司_华为 Python网络自动化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis集群监控及Redis桌面客户端
- 下一篇: httplib java_httplib