paramiko remote connection linux server upload and download files

Spent a lot of time studying paramiko in sftpclient file transfer, meal operate fierce as a tiger, and finally has been stuck in the path of error issues, access to information for reference bigwigs crazy experiences, or they might, sleep a lunch awakening to, a closer look at the original path of the specified file does not exist, then I go to buy a cool ice cream the next. Leaving the code for all to learn, it can be directly used on the line.

import paramiko

the Linux class (Object):

DEF the __init __ (Self, IP, username, password, timout = 30):
self.hostname = IP
self.username = username
self.password = password
self.port = 22 is
self.t = ''
Self. = Chan ''
# connection retries
self.try_times. 3 =
# invokes this method to connect a host linux
DEF Conn (Self):
Pass
# disconnect
DEF Close (Self):
Pass
the command sent to be executed #
def send (self) :
Pass
# upload
DEF sftp_put (Self, localfile, remotefile):
# set the remote IP address and port SSH connection
t = paramiko.Transport ((self.hostname, 22 ))
# Set username and password parameters
t.connect (= self.username username, password = self.password)
# instanced Transport SFTPClient passed as a parameter in the
SFTP = paramiko.SFTPClient.from_transport (T)
# of local test.txt put to a distal end, and remains test.txt
sftp.put (localfile, remotefile)
# close the connection
t.close ()
# download file
DEF sftp_get (Self, remotefile, newlocalfile):

T = paramiko.Transport (( self.hostname, 22 is))

t.connect (= self.username username, password = self.password)

SFTP = paramiko.SFTPClient.from_transport (T)
# test.txt put the distal end to the distal end, and remains newtest .txt
sftp.get (remotefile, newlocalfile)
# close the connection
t.close()



if __name__ == '__main__':
localpath = r'I:\Meitu\数据库密码.txt'
remotepath = r'/usr/local/test/数据库密码.txt'
newlocalpath = r'I:\Meitu\newtest.gz'
host = Linux('192.168.55.158', 'root', '1')
host.sftp_put(localpath,remotepath)
host.sftp_get(remotepath,newlocalpath)

Guess you like

Origin www.cnblogs.com/liuage/p/11016641.html