一、配置流程:
1.华为交换机配置:
aaa
local-user admin password cipher Huawei@123
local-user admin privilege level 15
local-user admin service-type ssh
stelnet server enable
ssh user admin
ssh user admin authentication-type all
ssh user admin service-type all
user-interface vty 0 4
authentication-mode aaa
user privilege level 15
protocol inbound all
2. pycharm代码编写:
//引入第三方模块
import paramiko
import time
//创建变量
ip = "192.168.126.12"
username = "admin"
password = "Huawei@123"
//使用第三方模块登录交换机cli界面
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
ssh_client.connect(hostname=ip, username=username, password=password, look_for_keys=False)
command = ssh_client.invoke_shell()
print("已成功登录交换机" + ip)
//在登录后的cli界面执行命令
command.send("sys\n")
command.send("sysname shiyan01\n")
command.send("interface loopback0\n")
command.send("ip address 1.1.1.1 32\n")
command.send("quit\nquit\n")
time.sleep(5)
//输出并打印回显信息
output = command.recv(65535).decode()
print(output)
//关闭ssh连接
ssh_client.close()
二、配置结果查看
1.交换机配置结果:
[shiyan01]dis ip in b
[shiyan01]dis ip in brief
*down: administratively down
^down: standby
(l): loopback
(s): spoofing
The number of interface that is UP in Physical is 3
The number of interface that is DOWN in Physical is 1
The number of interface that is UP in Protocol is 3
The number of interface that is DOWN in Protocol is 1
Interface IP Address/Mask Physical Protocol
LoopBack0 1.1.1.1/32 up up(s)
MEth0/0/1 unassigned down down
NULL0 unassigned up up(s)
Vlanif1 192.168.126.12/24 up up
2.pycharm回显结果:
C:\Users\17855\PycharmProjects\Network_shiyan\.venv\Scripts\python.exe C:\Users\17855\PycharmProjects\Network_shiyan\shiyan_01.py
已成功登录交换机192.168.126.12
Info: The max number of VTY users is 5, and the number
of current VTY users on line is 2.
The current login time is 2024-05-31 12:18:05.
<shiyan01interface loopback0>sys
Enter system view, return user view with Ctrl+Z.
[shiyan01interface loopback0]sysname shiyan01
[shiyan01]interface loopback0
[shiyan01-LoopBack0]ip address 1.1.1.1 32
[shiyan01-LoopBack0]quit
[shiyan01]quit
<shiyan01>
Process finished with exit code 0
源学习视频链接:《网络工程师的Python之路》 -- 第1期:初探Paramiko_哔哩哔哩_bilibili
感谢原作者的视频学习,本文章栏目是个人按学习视频学习记录的过程