1. 程式人生 > 其它 >Paramiko SSH登入華為裝置

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

d:\python/key_file_name

password

password

(金鑰儲存目錄d:\python要存在,否則會報錯;金鑰檔名稱自己取;金鑰目錄,密碼和確認密碼時都可以為空,直接按三次Enter也可以)

目錄d:\python下會生成一個私鑰,一個公鑰,以.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-hostrepo.huaweicloud.com
Writing to C:\Users\Administrator\AppData\Roaming\pip\pip.ini

C:\Users\Administrator>pip config set global.index-urlhttps://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-hostrepo.huaweicloud.com

pip config set global.index-urlhttps://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()