文章目录
- 1、安装环境并导入相关模块
- 2、创建VLAN并配置IP地址和路由协议
- 2.1 拓扑
- 2.2 简单配置与测试
- 2.3 python脚本
- 3、telnet 远程登录管理设备
- 3.1 拓扑
- 3.2 简单配置与测试
- 3.3 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")
运行结果:
困难越大,荣耀也越大。——西塞罗