python fabric paramiko 文件上传 下载

来源:互联网 发布:数据分析生命周期模型 编辑:程序博客网 时间:2024/05/01 10:55
import pexpectfrom fabric.api import env,put,get#import threadingclass Host:def __init__(self,ip,user,password):self.user=userself.ip=ipself.password=passworddef run(self,cmd):num=0ssh=paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())try:                        ssh.connect(self.ip,username=self.user,password=self.password)                except:                        print("connect to %s failed!" %self.ip)                        return 128                else:                    i,o,e=ssh.exec_command(cmd)                    if((e.read()).find("command not found")>0):                        print e.read()                        num=127                    elif((e.read()).find("no such file")>0 or cmd=="false"):                        num=1                    elif((e.read()).find("cannot access" or "unknown")>0):                        num=2                    print e.read()                    print o.read()                    return i,o,e,num        def upload(self,uSRC,uDST):            env.user=self.user            env.password=self.password            env.host_string=self.ip            put("%s" %(uSRC),"%s" %(uDST))            print("\n")        def download(self,dSRC,dDST):            env.user=self.user            env.password=self.password            env.host_string=self.ip            get("%s" %(dSRC),"%s" %(dDST))            print("\n")if __name__ == '__main__':t=Host("10.108.39.104","root","0000")t.run("asfssdfsg")#t.upload("/home/star/1.pdf","/home/star")#t.download("/home/star/NortonMobileSecurity-101.apk","/home/star/gcr")#t.run("echo "symc1234"|scp /usr/local/tomcat root@10.108.39.85:/home/chenzhipeng/solr")        

!/usr/bin/env python   import paramiko   import os   import datetime   hostname='10.108.39.85'username='root'password='0000'port=22local_dir='/usr/local/tomcat'remote_dir='/home/chenzhipeng/solr/'if __name__=="__main__":    #    try:           t=paramiko.Transport((hostname,port))           t.connect(username=username,password=password)           sftp=paramiko.SFTPClient.from_transport(t)   #        files=sftp.listdir(dir_path)           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)                           # sftp.get(os.path.join(dir_path,f),os.path.join(local_path,f))                   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:   #       print "error!"           t.close()    


关于fabric http://docs.fabfile.org/en/1.4.3/index.html#installation

安装文档: http://docs.fabfile.org/en/1.4.3/installation.html#downloads


from fabric.api import rundef host_type():    run('uname -s')

更多例子:http://hi.baidu.com/sebastianwade/item/a18f47c26416512de90f2e93python paramiko的安装:

安装需要
1、pycrypto 2、paramiko;


(1)首先安装: pycrypto
进入pycrypto的目录,执行:python setup.py install


pycrypto下载地址:
http://download.famouswhy.com/pycrypto/free_download.html

解决:
yum install python-devel

出现如下错误:

running build_ext
warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Hash.MD2' extension
creating build/temp.linux-i686-2.4
creating build/temp.linux-i686-2.4/src
gcc -pthread -fno-strict-aliasing -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -D_GNU_SOURCE -fPIC -fPIC -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/usr/include/python2.4 -c src/MD2.c -o build/temp.linux-i686-2.4/src/MD2.o
src/MD2.c:31:20: error: Python.h: No such file or directory
src/MD2.c:118: error: expected 鈥鈥 鈥鈥 鈥鈥 鈥榓sm鈥or 鈥榑_attribute__鈥before 鈥鈥token
In file included from src/MD2.c:134:
src/hash_template.c:42: error: expected specifier-qualifier-list before 鈥楶yObject_HEAD鈥
src/hash_template.c:46: error: expected 鈥鈥 鈥鈥 鈥鈥 鈥榓sm鈥or 鈥榑_attribute__鈥before 鈥楶yTypeObject鈥

2.安装 paramiko: (进入,paramiko的目录下)

python setup.py install


下载地址:
http://www.lag.net/paramiko/




备注:在window中的安装
http://hi.baidu.com/564857808/blog/item/00a00ed581aceb219b502783.html
http://www.voidspace.org.uk/python/modules.shtml#pycrypto
http://download.famouswhy.com/pycrypto/free_download.html

python paramiko的安装:

安装需要
1、pycrypto 2、paramiko;


(1)首先安装: pycrypto
进入pycrypto的目录,执行:python setup.py install


pycrypto下载地址:
http://download.famouswhy.com/pycrypto/free_download.html

解决:
yum install python-devel

出现如下错误:

running build_ext
warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Hash.MD2' extension
creating build/temp.linux-i686-2.4
creating build/temp.linux-i686-2.4/src
gcc -pthread -fno-strict-aliasing -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -D_GNU_SOURCE -fPIC -fPIC -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/usr/include/python2.4 -c src/MD2.c -o build/temp.linux-i686-2.4/src/MD2.o
src/MD2.c:31:20: error: Python.h: No such file or directory
src/MD2.c:118: error: expected 鈥鈥 鈥鈥 鈥鈥 鈥榓sm鈥or 鈥榑_attribute__鈥before 鈥鈥token
In file included from src/MD2.c:134:
src/hash_template.c:42: error: expected specifier-qualifier-list before 鈥楶yObject_HEAD鈥
src/hash_template.c:46: error: expected 鈥鈥 鈥鈥 鈥鈥 鈥榓sm鈥or 鈥榑_attribute__鈥before 鈥楶yTypeObject鈥

2.安装 paramiko: (进入,paramiko的目录下)

python setup.py install


下载地址:
http://www.lag.net/paramiko/

 


备注:在window中的安装
http://hi.baidu.com/564857808/blog/item/00a00ed581aceb219b502783.html
http://www.voidspace.org.uk/python/modules.shtml#pycrypto
http://download.famouswhy.com/pycrypto/free_download.html