Paramiko SSH私钥加密登录华为设备
在使用Git生成RSA私钥和公钥时,如果输入了密码,则对私钥进行了加密,
ssh-keygen -t rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
如果此处密码输入的是Huawei@123,则python代码改变如下:
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())
with open('rsa_pwd','r') as f:
private_key = paramiko.RSAKey.from_private_key(f,'Huawei@123')
f.close()
ssh.connect(hostname='192.168.56.188',port=22,username='python',pkey=private_key)
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()