一、实验拓扑:
要求:交换机 SW1 做为 SSH 服务端,桥接本地虚拟虚拟网卡;本地主机通过 python paramiko 库功能登录到 SW1 上进行配置备份;AR1 做为测试 SW1 的 SSH 客户端
二、实验环境搭建:
1、SW1 配置为 SSH 服务端:
1)在 aaa 视图下配置 SSH 用户名、密码、权限和服务类型:
aaa
authentication-scheme default
authorization-scheme default
accounting-scheme default
domain default
domain default_admin
local-user hw password cipher ;[KAPTGW:F-NZPO3JBXBHA!!
local-user hw privilege level 3
local-user hw service-type ssh
2)在 vty 接口下配置认证方式、允许的协议
user-interface vty 0 4
authentication-mode aaa
protocol inbound ssh
3)在全局模式下开启 stelnet 服务并配置允许的用户名、验证方式、用户的服务类型
stelnet server enable
ssh user hw
ssh user hw authentication-type password
ssh user hw service-type stelnet
2、AR1 配置 SSH 客户端
interface GigabitEthernet0/0/0
ip address 10.1.1.1 255.255.255.0
#
interface GigabitEthernet0/0/1
#
ssh client 10.1.1.2 assign rsa-key 10.1.1.2
ssh client first-time enable
#
- 注意需要允许第一次 SSH 客户端交互公钥,否则会报错:
Please run the command "ssh client first-time enable"to enable the first-time ac
cess function and try again.
3、Cloud1 配置桥接:
- 注意这里:SW1 连接的是虚拟网卡的内部 UDP 端口,而不是桥接端口 *
4、从 AR1 上测试 SW1 的 SSH 服务端功能:
stelnet 10.1.1.2
三、拓扑详细配置:
SW1:
aaa
authentication-scheme default
authorization-scheme default
accounting-scheme default
domain default
domain default_admin
local-user hw password cipher ;[KAPTGW:F-NZPO3JBXBHA!!
local-user hw privilege level 3
local-user hw service-type ssh
local-user admin password simple admin
local-user admin service-type http
#
interface Vlanif10
ip address 192.168.56.2 255.255.255.0
#
interface Vlanif20
ip address 10.1.1.2 255.255.255.0
#
interface GigabitEthernet0/0/1
port link-type access
port default vlan 10
interface GigabitEthernet0/0/24
port link-type access
port default vlan 20
#
stelnet server enable
ssh user hw
ssh user hw authentication-type password
ssh user hw service-type stelnet
#
user-interface con 0
user-interface vty 0 4
authentication-mode aaa
screen-length 0
protocol inbound ssh
#
AR1:
interface GigabitEthernet0/0/0
ip address 10.1.1.1 255.255.255.0
#
interface GigabitEthernet0/0/1
#
ssh client 10.1.1.2 assign rsa-key 10.1.1.2
ssh client first-time enable
四、python 程序搭建:
import paramiko
import time
user="hw"
pwd="hw123"
host="192.168.56.2"
ssh_client=paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
ssh_client.connect(hostname=host.strip(),username=user,password=pwd)
print("*"*60) #打印60个*号
cmd=ssh_client.invoke_shell() #创建一个shell会话
cmd.send("system\n")
cmd.send("user-interface vty 0 4\n")
cmd.send("screen-length 0\n")
cmd.send("dis curr\n")
time.sleep(3)
output = cmd.recv(99999).decode()
print(output)
ssh_client.close()
with open(host,"w") as filesv:
filesv.write(output)
五、pycharm 安装时的一些问题:
1、安装 paramiko 库:
pip install paramiko
a、在 cmd 窗口下:
检查是否安装了paramiko库:
pip show paramiko
b、在 pycharm 里安装:
文件 —> 设置 —> Python 解释器
2、安装中文汉化包:
文件 —> 设置 —> 插件 —> 搜索 Chinese(Simplicified) Language Pack 并安装
如果报错,可能 pip 插件版本比较老了,按照它的提示重新升级下 pip 插件:
python.exe -m pip install --upgrade pip
升级过后发现:中文插件地址找不到,只好去官网找了个相匹配的版本插件:
pycharm 汉化版本插件下载地址:
其他插件地址在这里:
Jetbrain插件搜索