paramiko免密登录

# -*- coding: utf-8 -*-
import paramiko


pkey='D:/pycharm_workspace/testpy/ssh_paramiko_demo/id_rsa'
key=paramiko.RSAKey.from_private_key_file(pkey)
paramiko.util.log_to_file('paramiko.log')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# ssh.load_system_host_keys()
ssh.connect('192.168.*.*',username = 'root',pkey=key)
stdin,stdout,stderr=ssh.exec_command('hostname')
print(stdout.read())
stdin,stdout,stderr=ssh.exec_command('ls')
print(stdout.readlines())

# -*- coding: utf-8 -*-  
import paramiko

pkey='D:/pycharm_workspace/testpy/ssh_paramiko_demo/id_rsa'  
key=paramiko.RSAKey.from_private_key_file(pkey) 
paramiko.util.log_to_file('paramiko.log')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
# ssh.load_system_host_keys()
ssh.connect('192.168.*.*',username = 'root',pkey=key) 
t = ssh.get_transport()
sftp = paramiko.SFTPClient.from_transport(t)
d = sftp.put("D:/pycharm_workspace/testpy/ssh_paramiko_demo/paramiko_pfile.py", "/root/aaaa.py")
print(d)
t.close()  

你可能感兴趣的:(paramiko免密登录)