Python + Paramiko implements sftp file upload and download

Recently, in the test server, I looked at the Paramiko module. I have always used the FileZilla tool. After thinking about it, continuous integration and updating the code can be done with Parmmiko, which is still good.

Paramiko is a module written in python language, which can connect to Linux server remotely, view the log status above, configure remote server in batches, upload files, download files, etc.

Initialize some parameters:

  1. host = "120.24.239.214"
  2. port = 22
  3. timeout = 30
  4. user = "root"
  5. password = "******"
  • 1
  • 2
  • 3
  • 4
  • 5

Paramiko executes linux commands remotely:

  1. # -*- coding:utf-8 -*-
  2. import paramiko
  3. def sftp_exec_command(command):
  4. try:
  5. ssh_client = paramiko.SSHClient()
  6. ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  7. ssh_client.connect(host, 22, user, password)
  8. std_in, std_out, std_err = ssh_client.exec_command(command)
  9. for line in std_out:
  10. print line.strip("\n")
  11. ssh_client.close()
  12. except Exception, e:
  13. print e
  14. if __name__ == '__main__':
  15. sftp_exec_command("ls -l")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Paramiko upload file:

  1. # -*- coding:utf-8 -*-
  2. import paramiko
  3. def sftp_upload_file(server_path, local_path):
  4. try:
  5. t = paramiko.Transport((host, 22))
  6. t.connect(username=user, password=password)
  7. sftp = paramiko.SFTPClient.from_transport(t)
  8. sftp.put(local_path, server_path)
  9. t.close()
  10. except Exception, e:
  11. print e
  12. if __name__ == '__main__':
  13. sftp_upload_file("/root/bug.txt", "D:/bug.txt")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Paramiko download file:

  1. # -*- coding:utf-8 -*-
  2. import paramiko
  3. def sftp_down_file(server_path, local_path):
  4. try:
  5. t = paramiko.Transport((host, 22))
  6. t.connect(username=user, password=password)
  7. sftp = paramiko.SFTPClient.from_transport(t)
  8. sftp.get(server_path, local_path)
  9. t.close()
  10. except Exception, e:
  11. print e
  12. if __name__ == '__main__':
  13. sftp_down_file("/root/test.txt", "D:/text.txt")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Python also has a default ftplib module that defines the FTP class, which has limited functions and can be used to implement a simple ftp client for uploading or downloading files.

FTP related command operations:

  1. ftp.cwd(pathname) #Set the path of the current FTP operation
  2. ftp.dir () #Display file information in the directory
  3. ftp.nlst() #Get the files in the directory
  4. ftp.mkd(pathname) #Create a new remote directory
  5. ftp.pwd() #Return the current location
  6. ftp.rmd(dirname) #delete remote directory
  7. ftp.delete(filename) #delete remote file
  8. ftp.rename(fromname, toname) #Change fromname to toname.
  9. ftp.storbinaly( "STOR filename.txt" ,file_handel,bufsize) #Upload target file
  10. ftp.retrbinary( "RETR filename.txt" ,file_handel,bufsize) #Download FTP file
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Some small examples:

  1. # -*- coding:utf-8 -*-
  2. from ftplib import FTP, all_errors
  3. import paramiko
  4. import sys, os
  5. import getpass
  6. def ftp_main():
  7. try:
  8. f = FTP("ftp.ibiblio.org")
  9. print f.getwelcome()
  10. f.login()
  11. print f.pwd()
  12. f.quit()
  13. except Exception, e:
  14. print all_errors, e
  15. def ftp_down_file():
  16. try:
  17. f = FTP("ftp.kernel.org")
  18. f.login()
  19. f.cwd("/pub/linux/kernel/v1.0")
  20. fd = open("patch8.gz", "wb")
  21. # Download the file in ASCII mode
  22. # f.retrlines("RETE patch8.gz", writeline)
  23. # Download the file in binary mode
  24. f.retrbinary("RETE patch8.gz", fd.write)
  25. fd.close()
  26. f.quit()
  27. except Exception, e:
  28. print all_errors, e
  29. def ftp_up_file():
  30. try:
  31. host2, username, local_path, server_path = sys.argv[1:]
  32. password2 = getpass.getpass("Enter passworf for %s on %s:" % (username, host2))
  33. f = FTP(host2)
  34. f.login(user=username, passwd=password2)
  35. f.cwd(server_path)
  36. fd = open(local_path, "rb")
  37. f.storbinary("STOR %s" % os.path.basename(local_path), fd)
  38. fd.close()
  39. f.quit()
  40. except Exception, e:
  41. print all_errors, e
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

Summarize:

Parmmiko completes the sftp file transfer operation very well, and can be used for continuous integration development or testing in SVN management code projects

Original link: http://blog.csdn.net/Temanm/article/details/50607741


There is also a more detailed article here, which is shared here to help deepen understanding.

The paramiko module provides the functions of ssh and sft to remotely log in to the server to execute commands and upload and download files. This is a third-party software package that needs to be installed before use.

1 sshclient login based on username and password

  1. # Create a sshclient object
  2. ssh = paramiko.SSHClient()
  3. # Allow trusted hosts to be automatically added to the host_allow list, this method must be placed in front of the connect method
  4. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  5. # Call the connect method to connect to the server
  6. ssh.connect(hostname='192.168.2.129', port=22, username='super', password='super')
  7. # Excuting an order
  8. stdin, stdout, stderr = ssh.exec_command('df -hl')
  9. # The result is put in stdout , if there is an error, it will be put in stderr
  10. print(stdout.read().decode())
  11. # close the connection
  12. ssh.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2 The transport method login 
method based on user name and password 1 is a traditional operation of connecting to the server, executing commands, and closing. Sometimes it is necessary to log in to the server to perform multiple operations, such as executing commands and uploading/downloading files. Method 1 cannot be implemented , which can be operated as follows

  1. # Instantiate a transport object
  2. trans = paramiko.Transport(('192.168.2.129', 22))
  3. # establish connection
  4. trans.connect(username='super', password='super')
  5. # Specify the transport of the sshclient object as the above trans
  6. ssh = paramiko.SSHClient()
  7. ssh._transport = trans
  8. # Execute the command, the same as the traditional method
  9. stdin, stdout, stderr = ssh.exec_command('df -hl')
  10. print(stdout.read().decode())
  11. # close the connection
  12. trans.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3 SSHClient login based on public key

  1. # Specify the local RSA private key file. If a password is set when creating a key pair, the password is the set password. If not, you do not need to specify the password parameter
  2. pkey = paramiko.RSAKey.from_private_key_file('/home/super/.ssh/id_rsa', password='12345')
  3. # establish connection
  4. ssh = paramiko.SSHClient()
  5. ssh.connect(hostname='192.168.2.129',
  6. port=22,
  7. username='super',
  8. pkey=pkey)
  9. # Excuting an order
  10. stdin, stdout, stderr = ssh.exec_command('df -hl')
  11. # The result is put in stdout , if there is an error, it will be put in stderr
  12. print(stdout.read().decode())
  13. # close the connection
  14. ssh.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4 Login via key-based Transport

  1. # Specify the local RSA private key file. If a password is set when creating a key pair, the password is the set password. If not, you do not need to specify the password parameter
  2. pkey = paramiko.RSAKey.from_private_key_file('/home/super/.ssh/id_rsa', password='12345')
  3. # establish connection
  4. trans = paramiko.Transport(('192.168.2.129', 22))
  5. trans.connect(username='super', pkey=pkey)
  6. # Specify the transport of the sshclient object as the above trans
  7. ssh = paramiko.SSHClient()
  8. ssh._transport = trans
  9. # Execute the command, the same as the traditional method
  10. stdin, stdout, stderr = ssh.exec_command('df -hl')
  11. print(stdout.read().decode())
  12. # close the connection
  13. trans.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

##### File transfer SFTP ###########

  1. # instantiate a transport object # instantiate a transport object
  2. trans = paramiko.Transport(('192.168.2.129', 22))
  3. # establish connection
  4. trans.connect(username='super', password='super')
  5. # Instantiate a sftp object and specify the connection channel
  6. sftp = paramiko.SFTPClient.from_transport(trans)
  7. # Send File
  8. sftp.put(localpath='/tmp/11.txt', remotepath='/tmp/22.txt')
  9. # download file
  10. # sftp.get(remotepath, localpath)
  11. trans.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

5 Realize the function of returning the result immediately after entering the command The 
above operations are all basic connections. If we want to implement a function similar to the xshell tool, after logging in, we can enter the command and press Enter to return the result:

  1. import paramiko
  2. import them
  3. import select
  4. import sys
  5. # create a socket
  6. trans = paramiko.Transport(('192.168.2.129', 22))
  7. # start a client
  8. trans.start_client()
  9. # if using rsa key to log in
  10. '''
  11. default_key_file = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa')
  12. prikey = paramiko.RSAKey.from_private_key_file(default_key_file)
  13. trans.auth_publickey(username='super', key=prikey)
  14. '''
  15. # If logged in with username and password
  16. trans.auth_password(username='super', password='super')
  17. # open a channel
  18. channel = trans.open_session()
  19. # get terminal
  20. channel.get_pty()
  21. # Activate the terminal, so you can log in to the terminal, just like we log in to the system with something similar to xshell
  22. channel.invoke_shell()
  23. # The following can perform all your operations, using select to achieve
  24. # Monitor the input terminal sys.stdin and channel,
  25. # When the user enters a command in the terminal, the command is given to the channel channel. At this time, sys.stdin changes, and select can sense it.
  26. # The process of sending commands and obtaining results of a channel is actually the process of sending and receiving information on a socket
  27. while True:
  28. readlist, writelist, errlist = select.select([channel, sys.stdin,], [], [])
  29. # If the user enters a command, sys.stdin changes
  30. if sys.stdin in readlist:
  31. # Get the input content
  32. input_cmd = sys.stdin.read(1)
  33. # send the command to the server
  34. channel.sendall(input_cmd)
  35. # The server returns the result, the channel channel receives the result, and the change is detected by select
  36. if channel in readlist:
  37. # get the result
  38. result = channel.recv(1024)
  39. # exit after disconnecting
  40. if len(result) == 0:
  41. print("\r\n**** EOF **** \r\n")
  42. break
  43. # output to screen
  44. sys.stdout.write(result.decode())
  45. sys.stdout.flush()
  46. # close the channel
  47. channel.close()
  48. # close link
  49. trans.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

6 Support tab auto-completion

  1. import paramiko
  2. import them
  3. import select
  4. import sys
  5. import tty
  6. import termios
  7. '''
  8. Realize the effect of an xshell login system, log in to the system and continuously enter commands and return results
  9. Support auto-completion, directly call the server terminal
  10. '''
  11. # create a socket
  12. trans = paramiko.Transport(('192.168.2.129', 22))
  13. # start a client
  14. trans.start_client()
  15. # if using rsa key to log in
  16. '''
  17. default_key_file = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa')
  18. prikey = paramiko.RSAKey.from_private_key_file(default_key_file)
  19. trans.auth_publickey(username='super', key=prikey)
  20. '''
  21. # If logged in with username and password
  22. trans.auth_password(username='super', password='super')
  23. # open a channel
  24. channel = trans.open_session()
  25. # get terminal
  26. channel.get_pty()
  27. # Activate the terminal, so you can log in to the terminal, just like we log in to the system with something similar to xshell
  28. channel.invoke_shell()
  29. # Get the properties of the original operating terminal
  30. oldtty = termios.tcgetattr(sys.stdin)
  31. try:
  32. # Set the current operation terminal attribute to the native terminal attribute on the server, which can support tab
  33. tty.setraw(sys.stdin)
  34. channel.settimeout(0)
  35. while True:
  36. readlist, writelist, errlist = select.select([channel, sys.stdin,], [], [])
  37. # If the user enters a command, sys.stdin changes
  38. if sys.stdin in readlist:
  39. # Get the content of the input, enter a character to send 1 character
  40. input_cmd = sys.stdin.read(1)
  41. # send the command to the server
  42. channel.sendall(input_cmd)
  43. # The server returns the result, the channel channel receives the result, and the change is detected by select
  44. if channel in readlist:
  45. # get the result
  46. result = channel.recv(1024)
  47. # exit after disconnecting
  48. if len(result) == 0:
  49. print("\r\n**** EOF **** \r\n")
  50. break
  51. # output to screen
  52. sys.stdout.write(result.decode())
  53. sys.stdout.flush()
  54. finally:
  55. # After execution, restore the current terminal properties to the original operating terminal properties
  56. termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
  57. # close the channel
  58. channel.close()
  59. # close link
  60. trans.close()
Original link: http://www.ifindbug.com/doc/id-44934/name-paramiko of python module.html


In fact, watching these Chinese version of the tutorial, if you can solve your problem, it is indeed more convenient and fast. However, once you encounter a problem and various abnormalities occur, you will not know how to solve it after reading these Chinese tutorials, so you still need to develop a search for the Internet at the beginning to see what methods are available (which modules of python, Which methods) can solve similar problems, and then consult the specific official API, so that the specific methods, parameters, and usage methods are clear at a glance, and the memory will be more profound, and there will be more sense of accomplishment after solving the problem. Therefore, it is more advisable to go to the official API to check the specific usage. When the problem cannot be solved, don’t go back and look at the official API again. I believe that the clues of the problem will be found here, so as to solve the problem. of.

Related: Python + Paramiko implements sftp file upload and download