华为 Python网络自动化

艺博东 2021-02-23 01:43:19
自动化 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")

运行结果:

在这里插入图片描述

困难越大,荣耀也越大。——西塞罗


在这里插入图片描述

好了这期就到这里了,如果你喜欢这篇文章的话,请点赞评论分享收藏,如果你还能点击关注,那真的是对我最大的鼓励。谢谢大家,下期见!

版权声明
本文为[艺博东]所创,转载请带上原文链接,感谢
https://ybd99.blog.csdn.net/article/details/113754754

  1. Python 空间绘图 - 房价气泡图绘制
  2. Translation: practical Python Programming 02_ 02_ Containers
  3. Research on Portfolio Optimization Based on particle swarm optimization
  4. Ubuntu deploying Django project
  5. Two years of Java, write Python and go without byte beating
  6. Translation: practical Python Programming 02_ 02_ Containers
  7. So learn python, grandfather learned! Introduction to super simple Python
  8. python3 多线程 与 mongo亿级消费日志数据 新鲜demo 【优化第一版】
  9. Summary of Chinese word segmentation based on Jieba
  10. I've heard it n times, but I'm not impressed. After reading this, you'll understand
  11. Summary of Chinese word segmentation based on Jieba
  12. From movie art to Python code to realize God's reverse thinking mode
  13. Summary of Chinese word segmentation based on Jieba
  14. ARIMA模型预测CO2浓度时间序列-python实现
  15. Python belongs to back-end development or front-end development? Introduction to Python!
  16. python isinstance()
  17. I've heard it n times, but I'm not impressed. After reading this, you'll understand
  18. This article will familiarize you with the transformation process of Python - & gt; cafe - & gt; om model
  19. 如何用Python一键修改上万个文件名
  20. One day quick start to Python
  21. Python 学习笔记: List
  22. 翻译:《实用的Python编程》02_03_Formatting
  23. Is there any age requirement for learning Python? Is 30 OK?
  24. Professor Tsinghua! The most complete Python tutorial in 12 hours (free sharing at the end of the article)
  25. Using Python to develop defi project
  26. Detailed explanation of Python function
  27. Python 可变类型作为函数默认参数时的副作用
  28. What do Python engineers do? What's their future?
  29. 这是我见过最好的Python教程:十分钟带你认识Python
  30. Python欢喜冤家:爬虫与反爬虫带着处理方案来给大家拜年了
  31. Python - zip() function
  32. 写Python会遇到如下的错误:ModuleNotFoundError: No module named 'email.mime'; 'email' is not a package
  33. Python类的调用以及私有和公有属性方法的调用
  34. Python类的专有方法
  35. Python基础之:数字字符串和列表
  36. How did Python pioneers evaluate this language on their 30th birthday?
  37. Python基础之:数字字符串和列表
  38. Python基础之:数字字符串和列表
  39. 窥探未来不是梦,python数据分析轻松实现
  40. This article will familiarize you with the transformation process of Python - & gt; cafe - & gt; om model
  41. 阿里、华为Python工程师总结的实用技巧,只有你还没看?
  42. 酸了!看到抖音上Python程序员晒得工资条......
  43. Python基础之:数字字符串和列表
  44. Importing excel into database adaptively by Python
  45. Python安装教程
  46. Python安装教程
  47. From Xiaobai to master, here is a guide to pandas
  48. [Python] drawing method of stem leaf diagram and compound pie diagram
  49. Drawing of Python geoplot spatial kernel density estimation map
  50. Python Seaborn economist's classic chart imitation
  51. Python space drawing - regionmask mask operation example
  52. Python space drawing - cartopy longitude and latitude add
  53. Python pykrige package Kriging interpolation calculation and visual rendering
  54. Python batch resampling, mask, slope extraction
  55. Python - Analysis of reachable circle of multiple traffic modes
  56. Python space drawing bubble drawing
  57. Python 3 multithreading and Mongo 100 million consumption log data fresh demo
  58. ARIMA model for predicting time series of CO2 concentration
  59. python isinstance()
  60. How to modify tens of thousands of file names with one key in Python