python模块paramiko的上传下载和远程执行命令方法

1:连接远程linux主机并执行命令

 
 
  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()  

执行结果如下:

 
 
  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:连接远程linux主机上传下载文件(paramiko模块是用SFTP协议来实现的)

 
 
  1. #!/usr/bin/env python  
  2. import paramiko,datetime,os 
  3. hostname='192.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 'Beginning 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))#下载               
  21.         #sftp.put(os.path.join(local_dir,f),os.path.join(remote_dir,f))#上传                 
  22.         print 'Download file success %s ' % datetime.datetime.now()        
  23.         print ''                  
  24.         print '##########################################'   
  25.     t.close() 
  26. except Exception:  
  27.        print "connect error!"  

执行结果:

 
 
  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. ##########################################  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值