How to upload, download, and remotely execute commands for the python module paramiko

Source: Internet
Author: User

Using python to remotely log on to the host and execute commands or upload and download files through sftp, there is a good module paramiko module to demonstrate these functions, which is very convenient to use, you can learn. I wrote several small programs to illustrate how to use this module.

1: connect to a remote linux host and execute the command

 
 
  1. #!/usr/bin/env python 
  2. import paramiko   
  3. hostname='192.168.0.102'  
  4. username='root'  
  5. password='abc'   
  6. port=22     
  7. paramiko.util.log_to_file('paramiko.log')          
  8. s=paramiko.SSHClient()                 
  9. s.set_missing_host_key_policy(paramiko.AutoAddPolicy())          
  10. s.connect(hostname = hostname,port=port,username=username, password=password)          
  11. stdin,stdout,stderr=s.exec_command('free;df -h')          
  12. print stdout.read()          
  13. s.close()  

The execution result is as follows:

 
 
  1.              total       used       free     shared    buffers     cached 
  2. Mem:       2074940    2057420      17520          0      42416    1867968 
  3. -/+ buffers/cache:     147036    1927904 
  4. Swap:      2096472        240    2096232 
  5. Filesystem            Size  Used Avail Use% Mounted on 
  6. /dev/sda1              30G   12G   17G  42% / 
  7. none                 1014M     0 1014M   0% /dev/shm 
  8. /dev/sda3             2.0G  289M  1.6G  16% /var 
  9. /dev/sdb1             135G   14G  115G  11% /data 
  10. /dev/sdc1             135G  127G  880M 100% /data1 
  11. /dev/sdd1             135G   99G   30G  78% /data2 

2: connecting to a remote linux host to upload and download the file paramiko module is implemented using SFTP protocol)

 
 
  1. #! /Usr/bin/env python
  2. Import paramiko, datetime, OS
  3. Hostname = '1970. 168.0.102'
  4. Username = 'root'
  5. Password = 'abc123'
  6. Port = 22
  7. Local_dir = '/tmp /'
  8. Remote_dir = '/tmp/test /'
  9. Try:
  10. T = paramiko. Transport (hostname, port ))
  11. T. connect (username = username, password = password)
  12. Sftp = paramiko. SFTPClient. from_transport (t)
  13. # Files = sftp. listdir (dir_path)
  14. Files = sftp. listdir (remote_dir)
  15. For f in files:
  16. Print''
  17. Print '###################################### ###'
  18. Print 'ininning to download file from % s % s' % (hostname, datetime. datetime. now ())
  19. Print 'downloading file: ', OS. path. join (remote_dir, f)
  20. Sftp. get (OS. path. join (remote_dir, f), OS. path. join (local_dir, f) # download
  21. # Sftp. put (OS. path. join (local_dir, f), OS. path. join (remote_dir, f) # upload
  22. Print 'Download file success % s' % datetime. datetime. now ()
  23. Print''
  24. Print '###################################### ####'
  25. T. close ()
  26. Failed t Exception:
  27. Print "connect error! "

Execution result:

 
 
  1. #########################################  
  2. Beginning to download file  from 192.168.0.102  2012-11-05 15:49:01.334686  
  3. Downloading file: /tmp/test/wgetrc Download file success 2012-11-05 15:49:05.955184   
  4. ##########################################   
  5.  
  6. #########################################  
  7. Beginning to download file  from 192.168.0.102  2012-11-05 15:49:05.955342  
  8. Downloading file: /tmp/test/xinetd.conf Download file success 2012-11-05 15:49:10.929568   
  9. ##########################################   
  10.  
  11. #########################################  
  12. Beginning to download file  from 192.168.0.102  2012-11-05 15:49:10.929740  
  13. Downloading file: /tmp/test/warnquota.conf Download file success 2011-12-05 15:49:14.213570   
  14. ##########################################  

There are a lot of usage, specific can see the official documentation: http://www.lag.net/paramiko/docs/

This article from the "Wang Wei" blog, please be sure to keep this source http://wangwei007.blog.51cto.com/68019/1058726

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.