使用paramiko来上传文件到远端服务器

需求:

使用paramiko来批量上传文件到Linux远端服务器  

脚本如下:

[root@salt_server paramiko]# cat paramiko-upload.py 
#!/bin/env python
##paramiko download files

import paramiko
import os
import datetime


hostname='192.168.100.228'
username='root'
password='123456'
port=22
localpath = '/tmp/'
remotepath = '/tmp/test/'

if __name__=="__main__":
    ssh=paramiko.Transport((hostname,port))
    ssh.connect(username=username,password=password)
    sftp=paramiko.SFTPClient.from_transport(ssh)
    files=os.listdir(localpath)
    for file  in files:
        print ''
        print '### ### ### ### ### ### ### ### ### ### #'
        print "Begining to upload file%s" % datetime.datetime.now()
        print "Uploading file:",os.path.join(localpath,file)


        sftp.put(os.path.join(localpath,file),os.path.join(remotepath,file))
        print "Upload file success %s" % datetime.datetime.now()
        print '### ### ### ### ### ### ### ### ### ### '
    ssh.close()
    <!--more-->

测试: PS:在tmp下不能有目录 如果有目录会提示报错

[root@salt_server paramiko]# python paramiko-upload.py 

### ### ### ### ### ### ### ### ### ### #
Begining to upload file2014-11-14 22:13:53.058372
Uploading file: /tmp/test/messages
Upload file success 2014-11-14 22:13:53.061104
### ### ### ### ### ### ### ### ### ### 
[root@salt_server paramiko]# 
OpsNotes wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!