哈喽,大家好!我是艺博东 ,是一个思科出身、专注于华为的网工;好了,话不多说,我们直接进入正题。
光棍二十年,不知道情人节是什么鬼东西。还是好好学技术吧!努力、奋斗吧!为了早日走向人生巅峰,迎娶白富美!拼了
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
<font color="FF0000">注意:</font>做桥记得关闭防火墙,否则 eNSP PING 测不通物理机。
2.3 python 脚本
import paramikoimport 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 130command.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 telnetlibimport timehost ='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 = UserTagprint(response.decode('ascii'))time.sleep(2)
tn.write(b"dir\n")response = UserTagif b'>' in response: print(response.decode('ascii'))time.sleep(2)
tn.write(b"system-view\n")SysTag = tn.read_until(_SysTag.encode('ascii'))response = SysTagprint(response.decode('ascii'))time.sleep(2)
tn.close()print("close")
复制代码
运行结果:
>困难越大,荣耀也越大。——西塞罗
---------------------------------------------------------------------------------------------------------------------
好了这期就到这里了,如果你喜欢这篇文章的话,请点赞评论分享收藏,如果你还能点击关注,那真的是对我最大的鼓励。谢谢大家,下期见!
评论