python fabric paramiko 文件上传 下载
2013-08-30 14:51
489 查看
import pexpect from fabric.api import env,put,get #import threading class Host: def __init__(self,ip,user,password): self.user=user self.ip=ip self.password=password def run(self,cmd): num=0 ssh=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=22 local_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 run def 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
相关文章推荐
- python使用paramiko和SCPClient进行linux远程控制和上传和下载文件
- python paramiko实现多线程远程执行命令、多线程远程上传文件、多线程远程下载文件
- Python paramiko模块 实现 ssh远程执行命令 上传下载文件 堡垒机模式下的远程命令执行
- 利用python fabric模块写的批量操作远程主机脚本(命令执行,上传、下载文件)
- Python + Paramiko实现sftp文件上传下载
- Python + Paramiko实现sftp文件上传下载
- python paramiko实现多线程远程执行命令、多线程远程上传文件、多线程远程下载文件
- python paramiko模块实现sftp上传下载文件
- python模块学习之paramiko远程执行命令,文件上传、下载
- Python paramiko SFTP协议上传下载文件
- python模块学习之paramiko远程执行命令,文件上传、下载
- python实现socket上传下载文件-进度条显示
- Python在使用FTP下载上传文件时遇到中文名称和路径时的解决办法
- Python实现简单的HTTP服务器(支持文件上传下载)
- Python Fabric 模块 打包上传文件及检查 批量安装lnmp环境 版本发布及回滚
- python ftp上传下载文件,支持断点续传
- python->FTP上传下载文件
- 如何使用 paramiko 模块来实现远程执行OS命令和文件上传下载?
- python实现socket上传下载文件-进度条显示