paramiko 基于密钥文件登陆

首先密钥登陆远程的原理

client 端 将公钥放在远程机器authorized_keys:

使用 ssh-copy-id  app@ip

接着在client机器生成密钥 使用ssh-keygen -t  rsa 生成密钥对,id_rsa:

编写测试脚本:

import paramiko
hostname="192.168.110.131"
port=22
username="app"
#client ssh-keygen -t rsa 生成的id_rsa 私钥 pkey_path="/home/app/.ssh/id_rsa" private = paramiko.RSAKey.from_private_key_file(pkey_path) paramiko.util.log_to_file('paramiko.log') client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname,port,username,pkey=private) stdin, stdout, stderr = client.exec_command('df -h ') print(stdout.read().decode('utf-8'))

  

猜你喜欢

转载自www.cnblogs.com/SunshineKimi/p/12345310.html