Huawei Python Network Automation

Yibodong 2021-02-23 03:07:04
huawei python network automation


hello , Hello everyone ! I am a Yibodong , I'm from Cisco 、 Focus on Huawei's network workers ; Okay , Don't talk much , Let's get straight to the point .

Twenty years of Bachelor , I don't know what the hell Valentine's Day is . You'd better learn technology ! Strive 、 Fight ! In order to reach the peak of life as soon as possible , Marry Bai Fumei ! Spell it.

1、 Install the environment and import the relevant modules

The first is to install Python3 Environmental Science , Then install Paramiko modular , Then input pip3 install paramiko.

 Insert picture description here

pip3 install paramiko
 Insert picture description here

 Insert picture description here

 Insert picture description here

to update pip

pip install --upgrade pip

 Insert picture description here
 Insert picture description here
OK

Get into python, Import paramiko modular

import paramiko
 Insert picture description here

2、 establish VLAN And configuration IP Address and routing protocol

2.1 Topology

 Insert picture description here

2.2 Simple configuration and testing

 Insert picture description here

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 Router AR1 Of PING Test the physical machine

[SW1]ping 192.168.117.1
 Insert picture description here

The physical machine PING test eNSP Router AR1
 Insert picture description here

Be careful : Remember to close the firewall , otherwise eNSP PING Can't test the physical machine .

 Insert picture description here

2.3 python Script

import paramiko
import time
ip = "192.168.117.254" # Switchboard IP Address 
user = "ybd" #SSH Username 
pw = "1008611" #SSH Password 
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ip, username=user , password=pw)
print(" Successfully logged in to eNSP It's on the switch !" , ip)
# After successful connection , call invoke_shell() Method to wake up shell, That is the command line of Huawei system , And assign it to command, Convenient for subsequent calls .
command = ssh.invoke_shell()
# Send commands to the device , Commands that need to be executed .
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") # establish vlan 130
command.send("quit \n") # Return to upper level 
command.send("int vlan 130 \n") # Get into vlan 130 View 
command.send("ip add 192.168.130.1 24 \n") # To configure IP Address 
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")
# Use sleep function , Let's take a rest after the steps are executed 2s, Echo the content again .65535 How many characters are echoed 
time.sleep(3)
output = command.recv(65535)
print(output.decode("ascii"))
ssh.close() # After the configuration , use close Method of ssh

Running results :

 Insert picture description here

[SW1]display ip int brief
 Insert picture description here
 Insert picture description here

has OK

3、telnet Remote login management device

3.1 Topology

 Insert picture description here

3.2 Simple configuration and testing

 Insert picture description here

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 Router AR1 Of PING Test the physical machine

[AR1]ping 192.168.150.1 Insert picture description here

The physical machine PING test eNSP Router AR1
 Insert picture description here

3.3 python Script

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")

Running results :

 Insert picture description here

The more difficult it is , The greater the glory .—— Cicero


 Insert picture description here

Well, that's the end of the issue , If you like this article , Please like comments and share collections , If you can still click follow , That's really the biggest encouragement for me . Thank you. , See you next time !

版权声明
本文为[Yibodong]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/02/20210223014303732L.html

  1. Python 学习笔记: List
  2. 翻译:《实用的Python编程》02_03_Formatting
  3. Is there any age requirement for learning Python? Is 30 OK?
  4. Professor Tsinghua! The most complete Python tutorial in 12 hours (free sharing at the end of the article)
  5. Using Python to develop defi project
  6. Detailed explanation of Python function
  7. Python 可变类型作为函数默认参数时的副作用
  8. What do Python engineers do? What's their future?
  9. 这是我见过最好的Python教程:十分钟带你认识Python
  10. Python欢喜冤家:爬虫与反爬虫带着处理方案来给大家拜年了
  11. Python - zip() function
  12. 写Python会遇到如下的错误:ModuleNotFoundError: No module named 'email.mime'; 'email' is not a package
  13. Python类的调用以及私有和公有属性方法的调用
  14. Python类的专有方法
  15. Python基础之:数字字符串和列表
  16. How did Python pioneers evaluate this language on their 30th birthday?
  17. Python基础之:数字字符串和列表
  18. Python基础之:数字字符串和列表
  19. 窥探未来不是梦,python数据分析轻松实现
  20. This article will familiarize you with the transformation process of Python - & gt; cafe - & gt; om model
  21. 阿里、华为Python工程师总结的实用技巧,只有你还没看?
  22. 酸了!看到抖音上Python程序员晒得工资条......
  23. Python基础之:数字字符串和列表
  24. Importing excel into database adaptively by Python
  25. Python安装教程
  26. Python安装教程
  27. From Xiaobai to master, here is a guide to pandas
  28. [Python] drawing method of stem leaf diagram and compound pie diagram
  29. Drawing of Python geoplot spatial kernel density estimation map
  30. Python Seaborn economist's classic chart imitation
  31. Python space drawing - regionmask mask operation example
  32. Python space drawing - cartopy longitude and latitude add
  33. Python pykrige package Kriging interpolation calculation and visual rendering
  34. Python batch resampling, mask, slope extraction
  35. Python - Analysis of reachable circle of multiple traffic modes
  36. Python space drawing bubble drawing
  37. Python 3 multithreading and Mongo 100 million consumption log data fresh demo
  38. ARIMA model for predicting time series of CO2 concentration
  39. python isinstance()
  40. How to modify tens of thousands of file names with one key in Python
  41. Python notes: List
  42. Translation: practical Python Programming 02_ 03_ Formatting
  43. Python中的四种队列(queue)、堆(heap)
  44. Side effects of Python mutable types as default parameters of functions
  45. This is the best Python tutorial I've ever seen: ten minutes to get to know python
  46. 使用python编写量子线路打印的简单项目,并使用Sphinx自动化生成API文档
  47. Python happy enemy: crawler and anti crawler with a solution to give you New Year
  48. 使用python编写量子线路打印的简单项目,并使用Sphinx自动化生成API文档
  49. When writing python, you will encounter the following error: modulenotfounderror: no module named ' email.mime '; 'email' is not a package
  50. Python class call and private and public property method call
  51. Proprietary methods for Python classes
  52. Foundation of Python: number string and list
  53. Foundation of Python: number string and list
  54. Foundation of Python: number string and list
  55. 华为 Python网络自动化
  56. Python Cannot open E:\Python36\Scripts\pip-script.py
  57. Peeping into the future is not a dream, python data analysis is easy to achieve
  58. The practical skills summed up by Alibaba and Huawei Python engineers, only you haven't seen them yet?
  59. Sour! See the Python programmers on the tiktok get the pay slip...
  60. Foundation of Python: number string and list