文章目录
实验拓扑图
基础配置
#sw1
sys
sy sw1
vlan batch 100
interface G0/0/1
port link-type trunk
port trunk allow-pass vlan 100
interface G0/0/2
port link-type trunk
port trunk allow-pass vlan 100
interface G0/0/24
port link-type trunk
port trunk allow-pass vlan 100
interface G0/0/3
port link-type access
port default vlan 100
stp enable
stp root primary #设置主根桥
interface vlanif 100
ip address 192.168.100.1 24
#sw
sys
sy sw2
vlan batch 100
interface G0/0/1
port link-type trunk
port trunk allow-pass vlan 100
interface G0/0/2
port link-type trunk
port trunk allow-pass vlan 100
interface G0/0/24
port link-type trunk
port trunk allow-pass vlan 100
interface vlanif 100
ip address 192.168.100.2 24
sys
sy sw3
vlan batch 100
interface G0/0/1
port link-type trunk
port trunk allow-pass vlan 100
interface G0/0/2
port link-type trunk
port trunk allow-pass vlan 100
interface vlanif 100
ip address 192.168.100.3 24
#sw4
sys
sy sw4
vlan batch 100
interface G0/0/1
port link-type trunk
port trunk allow-pass vlan 100
interface G0/0/2
port link-type trunk
port trunk allow-pass vlan 100
interface vlanif 100
ip address 192.168.100.4 24
交换机部署SSH
stelnet server enable
ssh user karl authentication-type password
ssh user karl service-type stelnet
# 开放 vty 端口,能被远程登录
# 开放 编号为 0 1 2 3 4 的 vty端口,同时支持五个远程用户连
user-interface vty 0 4
# 认证模式为 aaa---> authentication 认证(谁能登录进来)
# ---> authorizathon 授权(谁进来能干什么)
# ---> according 审计(谁进来干了什么
authentication-mode aaa
protocol inbound ssh # 登录进来 的协议是 ssh
# 三A认证配置
aaa
local-user karl password cipher Huawei@123 #设置密码
local-user karl privilege level 15 #用户登录等级
local-user karl service-type ssh #开启该用户允许使用SSH访问设备的权限
登陆到各台交换机,并为其配置vlan 11 to 15,保存配置并退出。
paramiko模块是基于Python实现的ssh远程安全链接,实现命令远程、文件传输、SSH代理等功能。
#Python脚本
import paramiko
import time
from device_info import *
# 需要一个数据库,存储着 各个设备的信息
for item in range(len(datebase)): # range(len(datebase)---> 0-->3
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#默认情况下,Paramiko会拒绝任何未知的SSH public keys,使用此函数使其接收来自交换机提供的public keys。
ssh.connect(hostname=datebase[item]["ip_add"], username=datebase[item]["username"],
password=datebase[item]["passwd"])
command = ssh.invoke_shell()
#调用invoke_shell()方法来唤醒shell,也就是华为系统命令行,同时把它赋值给command,方便后续调用。
with open(datebase[item]["path"], "r", encoding='utf-8') as f:
while True:
content = f.readline()
if not content:
break
command.send(content.encode()
print("please wait a monment...")
time.sleep(5) #使用sleep函数,让脚步执行后休息2s,再回显内容。65535是回显多少个字符
output = command.recv(65535)
print(output.decode())
# 程序运行结束提示!
done()
ssh.close()
#device_info.py
dict_SW1 = {"ip_add": "192.168.100.1", "username": "karl", "passwd": "Huawei@123", "path": "sw1.cfg"}
dict_SW2 = {"ip_add": "192.168.100.2", "username": "karl", "passwd": "Huawei@123", "path": "sw1.cfg"}
dict_SW3 = {"ip_add": "192.168.100.3", "username": "karl", "passwd": "Huawei@123", "path": "sw1.cfg"}
dict_SW4 = {"ip_add": "192.168.100.4", "username": "karl", "passwd": "Huawei@123", "path": "sw1.cfg"}
datebase = [dict_SW1, dict_SW2, dict_SW3, dict_SW4]
def done():
print("""
8
8
.oPYo8 .oPYo. odYo. .oPYo.
8 8 8 8 8' `8 8oooo8
8 8 8 8 8 8 8.
`YooP' `YooP' 8 8 `Yooo'
:.....::.....:..::..:.....:
:::::::::::::::::::::::::::
:::::::::::::::::::::::::::
""")
结果如下: