#!/usr/bin/env pythonImportParamikoImportSys,oshost='git' #IP地址或在hosts文件中绑定主机名IPUser='Root' #系统用户名Password='123' #密码s=Paramiko. Sshclient () #ssh实例s. Load_system_host_keys () #load本机的s. Set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) #防止以前没连接过客户端主机, which means that there is no record of the corresponding client host in the known_hosts on the server and the error T= Paramiko. Transport ((host,22) #主机地址和端口t. Connect (username=user,password=password) #用户名和密码#pkey_file = '/root/.ssh/id_rsa ' # before the script starts, the public key of the user who generates the connection is Ssh-keygen, and the public key is ssh-copy-id-i [client user]@[ip address]#key = Paramiko. RSAKey.from.private_key_file (pkey_file) #使用私钥连接#T.connect (username=user,pkey=key) #使用用户名和key进行连接sftp=Paramiko. Sftpclient.from_transport (t) sftp.get ('/root/git.pdf','git.pdf'# The client's /root/git.pdf is routed to the current directory where the server-side script resides and is named git.pdfsftp.put ('para.py','/root/para_ssh.py') #将server脚本所在目录的 The para.py file to the client's /root/para_ssh.pys.close
Python Paramiko sftp