Paramiko SSH登录华为设备

ALT + PrtSc  截图当前窗口

PrtSc  截图全屏

1、新建Topo如图:

交换机选择CE12800,加个Cloud,通过Copper连线。

 

 

 2、配置Cloud端口,先添加UDP端口GE1,再添加VirtualBox Host-Only Network桥接端口。

映射端口配置如Port Map Setting:入端口Local Port Num1,出端口Remote Port Num2,勾选双向通道

 

 

 

 

 

3、交换机配置:

<HUAWEI>sys
Enter system view, return user view with return command.
[~HUAWEI]sysn CE1
[*HUAWEI]int vlani 1
[*HUAWEI-Vlanif1]ip add 192.168.56.188 24
[*HUAWEI-Vlanif1]q
[*HUAWEI]com
[~CE1]ping 192.168.56.1

PING 192.168.56.1: 56 data bytes, press CTRL_C to break
Request time out
Request time out
Request time out
Request time out
Request time out

--- 192.168.56.1 ping statistics ---
5 packet(s) transmitted
0 packet(s) received
100.00% packet loss

配置后发现ping不通网关地址,查看故障原因是端口未启动。

启动端口:

[~CE1]int g1/0/0
[~CE1-GE1/0/0]un shutdown
[*CE1-GE1/0/0]com
[~CE1-GE1/0/0]q

[~CE1]ping 192.168.56.1

PING 192.168.56.1: 56 data bytes, press CTRL_C to break
Reply from 192.168.56.1: bytes=56 Sequence=1 ttl=128 time=12 ms
Reply from 192.168.56.1: bytes=56 Sequence=2 ttl=128 time=6 ms
Reply from 192.168.56.1: bytes=56 Sequence=3 ttl=128 time=5 ms
Reply from 192.168.56.1: bytes=56 Sequence=4 ttl=128 time=5 ms
Reply from 192.168.56.1: bytes=56 Sequence=5 ttl=128 time=5 ms

--- 192.168.56.1 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-trip min/avg/max = 5/6/12 ms

 

交换机配置思路:

1、 配置Stelnet:配置管理IP,使能Stelnet功能,配置用户界面

2、 配置用户:创建本地用户和SSH用户,配置服务类型和认证方式

3、 配置公钥:添加客户端生成的公钥并分配给用户

3.1 创建密钥对:本地生成公钥和私钥

3.2 编写python代码

3.3 结果验证

 具体配置:

使能交换机STelnet功能,配置VTY用户界面。

stelnet server enable

user-interface vty 0 4

authentication-mode aaa

protocol inbound ssh

user privilege level 3

q

创建本地用户python ,将用户加入管理员组,并配置用户服务方式

aaa

local-user python password irreversible-cipher Huawei@123

local-user python user-group manage-ug

local-user python service-type ssh

创建SSH用户,配置认证方式和服务方式

ssh user python

ssh user python authentication-type rsa

ssh user python service-type stelnet

 

windows系统下的cmd命令行是不能直接使用ssh命令的,

所以使用Git Bash 创建RSA密钥对,并将公钥拷贝到交换机

ssh-keygen -t rsa

Enter

Enter

Enter

(密钥保存目录要存在,否则会报错;密钥文件名称自己取;密钥目录,密码和确认密码时都可以为空,直接按三次Enter也可以)

目录下会生成一个私钥,一个公钥,以.pub结尾的是公钥。

 

 

 

 

显示公钥

cat /d/python/id_rsa_pwd.pub 

复制公钥

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCwWXfSl0VDxkncszZ4ZDl3bZcTeIscegyTXXlmXwD69DjVhSW+xsiLsJcrl6y9lUDO4LHHHeCXOaUXWhvs7GZI5AvPLAgWnSmt6vJXrhiDv86Qcj1F37GeEdmzXWnwSsoWr9ic73O/Hy+1KAYR7Cp6D4oRessbfmXLZRU/IgSrhWgat53JyWcF2LtTd1M+A4fODkNczf51OQVkAc12cDtWSryMVhT83k3tF3Vf9upeL2EGDaBLsuSPrr4Ss3Fu7wpVOW18gEDxtHveyxbOrgArG4qBdxdIe9dLLkZozEcm4bxQzZpkqKBuy71HChu6BbnS0wegg7JkjKiRxsRS1o4l administrator@geng

 

在交换机上添加公钥,并将公钥分配给用户

rsa peer-public-key rsa01 encoding-type openssh

public-key-code begin

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCwWXfSl0VDxkncszZ4ZDl3bZcTeIscegyTXXlmXwD69DjVhSW+xsiLsJcrl6y9lUDO4LHHHeCXOaUXWhvs7GZI5AvPLAgWnSmt6vJXrhiDv86Qcj1F37GeEdmzXWnwSsoWr9ic73O/Hy+1KAYR7Cp6D4oRessbfmXLZRU/IgSrhWgat53JyWcF2LtTd1M+A4fODkNczf51OQVkAc12cDtWSryMVhT83k3tF3Vf9upeL2EGDaBLsuSPrr4Ss3Fu7wpVOW18gEDxtHveyxbOrgArG4qBdxdIe9dLLkZozEcm4bxQzZpkqKBuy71HChu6BbnS0wegg7JkjKiRxsRS1o4l administrator@geng

 

public-key-code end

peer-public-key end

ssh user python assign rsa-key rsa01

 commit

 

 

安装paramiko

C:\Users\Administrator>pip config set global.trusted-host repo.huaweicloud.com
Writing to C:\Users\Administrator\AppData\Roaming\pip\pip.ini

C:\Users\Administrator>pip config set global.index-url https://repo.huaweicloud.com/repository/pypi/simple
Writing to C:\Users\Administrator\AppData\Roaming\pip\pip.ini

C:\Users\Administrator>pip config set global.timeout 120
Writing to C:\Users\Administrator\AppData\Roaming\pip\pip.ini

pyton源配置:

pip config set global.trusted-host repo.huaweicloud.com

pip config set global.index-url https://repo.huaweicloud.com/repository/pypi/simple

pip config set global.timeout 120

pip install paramiko

python -m ensurepip

 

python代码:

 

__author__ = 'Administrator'
# _*_ coding: UTF-8 _*_
# @Create by gengyu
# @Create Time :2021/12/5
# @File_name : python_ssh_paramiko
import paramiko
import time

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(hostname='192.168.56.188',port=22,username='python',key_filename=r'c:/Users/Administrator/.ssh/id_rsa.pub')

cmd = ssh.invoke_shell()
cmd.send('screen-length 0 temporary\n')
cmd.send('dis cu\n')
time.sleep(3)

show_res = cmd.recv(999999).decode()
print(show_res)
ssh.close()

 

 

 

 

posted @ 2021-12-05 14:16  火山的爱  阅读(355)  评论(0编辑  收藏  举报