技术专栏
华为 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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
<
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
<
运行结果:
[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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
<
运行结果:
困难越大,荣耀也越大。——西塞罗
好了这期就到这里了,如果你喜欢这篇文章的话,请点赞评论分享收藏,如果你还能点击关注,那真的是对我最大的鼓励。谢谢大家,下期见!
声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包
89
8
评论
打赏
- 分享
- 举报
评论
0个
手气红包

相关专栏
-
浏览量:1957次2018-06-14 12:35:25
-
浏览量:1830次2018-02-16 01:09:29
-
浏览量:1356次2023-02-14 10:01:43
-
浏览量:4894次2020-12-23 17:25:07
-
浏览量:1576次2019-08-02 14:58:25
-
浏览量:4017次2020-12-18 11:28:16
-
浏览量:1532次2022-12-15 10:15:36
-
浏览量:841次2023-09-08 09:39:37
-
浏览量:1373次2023-10-08 17:44:55
-
浏览量:2587次2020-07-22 15:15:47
-
浏览量:2015次2018-02-05 01:06:26
-
浏览量:2683次2020-12-04 14:00:16
-
浏览量:1152次2024-07-15 11:57:20
-
2021-05-18 14:53:37
-
浏览量:568次2023-09-19 17:48:26
-
浏览量:822次2024-03-05 16:31:54
-
浏览量:31421次2021-07-06 10:18:59
-
浏览量:1059次2023-03-01 11:22:14
-
浏览量:1737次2019-11-15 08:47:19
置顶时间设置
结束时间
删除原因
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
打赏作者
艺博东
您的支持将鼓励我继续创作!
打赏金额:
¥1

¥5

¥10

¥50

¥100

支付方式:

举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
审核成功
发布时间设置
发布时间:
请选择发布时间设置
是否关联周任务-专栏模块
审核失败
失败原因
请选择失败原因
备注
请输入备注