Python:paramiko模块
2016-07-06 14:32
351 查看
#!/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()
#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()
相关文章推荐
- 解决Ubuntu12.04安装python-dev时依赖出错的问题
- 怎样开始写第一个基于python的selenium脚本
- python 函数总结
- Python 变量学习
- python 字符串
- python break,pass,continue 循环语句
- python for 循环
- Python生成进程方法2-Pool
- python while循环
- 详解rsyslog/Python/LogAnalyzer 记录和查看服务端/客户端日志
- python 条件语句
- Python编程语言
- Window环境下Python和Django的安装,以及项目的创建
- 招聘python工程师
- GitHub上Top20个 Python 语言机器学习项目
- python爬虫 登陆豆瓣 爬豆瓣电影短评
- python自动化测试,生成测试报告
- Python取出列表相应值的位置(表处理)
- Python网页信息采集:使用PhantomJS采集淘宝天猫商品内容
- Python创建进程1-Process方法实现