Python:paramiko模块


#!/usr/bin/env python  
#coding=utf-8

import os  
import paramiko  
import datetime 

#远程服务器  
hostname = '192.168.1.xxx'  
port = xxx
username = 'fangyali'
password = 'xxx' 
local_dir='/home/fangyali/test' 
remote_dir='/tmp/fangyali' 

def ssh():
    try:
        s = paramiko.SSHClient()  
        #读取know_host  
        #s.load_system_host_keys()  
        s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        #建立SSH连接  
        s.connect(hostname,port,username,password)  
        stdin,stdout,stderr = s.exec_command('/sbin/ifconfig;free;df -h')  
        #打印标准输出  
        print stdout.read()  
    except Exception,e:
        print "error!",e
    finally:
        s.close() 



def upload():
    try: 
        t=paramiko.Transport((hostname,port)) 
        t.connect(username=username,password=password) 
        sftp=paramiko.SFTPClient.from_transport(t) 
        #本地使用os模块,远端使用sftp.listdir(remote_dir) 
        files=os.listdir(local_dir) 
        for f in files: 
            print '' 
            print '#########################################' 
            print 'Beginning to upload file %s ' % datetime.datetime.now() 
            print 'Uploading file:',os.path.join(local_dir,f) 
    
            #上传put:local-remote,下载get:remote-local
            sftp.put(os.path.join(local_dir,f),os.path.join(remote_dir,f)) 
    
            print 'Upload file success %s ' % datetime.datetime.now() 
            print '' 
            print '##########################################' 

    except Exception,e: 
        print "error!",e
    finally:
        t.close() 




def download():
    try: 
        t=paramiko.Transport((hostname,port)) 
        t.connect(username=username,password=password) 
        sftp=paramiko.SFTPClient.from_transport(t) 
        #本地使用os模块,远端使用sftp.listdir(remote_dir) 
        files=sftp.listdir(remote_dir) 
        for f in files: 
            print '' 
            print '#########################################' 
            print 'Beginning to download file  from %s  %s ' % (hostname,datetime.datetime.now()) 
            print 'Downloading file:',os.path.join(remote_dir,f) 
    
            sftp.get(os.path.join(remote_dir,f),os.path.join(local_dir,f)) 
            #上传put:local-remote,下载get:remote-local
    
            print 'Download file success %s ' % datetime.datetime.now() 
            print '' 
            print '##########################################' 

    except Exception,e:
        print "error!",e 
    finally:
        t.close() 


def download_one():
    try: 
        t=paramiko.Transport((hostname,port)) 
        t.connect(username=username,password=password) 
        sftp=paramiko.SFTPClient.from_transport(t) 
        sftp.get("/tmp/fangyali/mysql_status.txt3306","/home/fangyali/test/1.txt") 

    except Exception,e:
        print "error!",e
    finally:
        t.close() 

ssh()
upload()
download()





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值