python paramiko模块实现sftp上传下载文件
2016-05-20 17:24
786 查看
用python实现远程登陆主机执行命令或通过sftp上传下载文件,有个很好的模块paramiko模块来演示这些功能,使用起来很方便,大家可学习一下。写了几个小程序,用于说明此模块的使用方法。
1:连接远程linux主机并执行命令
#!/usr/bin/env python
import paramiko
hostname='192.168.0.102'
username='root'
password='abc'
port=22
paramiko.util.log_to_file('paramiko.log')
s=paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname = hostname,port=port,username=username, password=password)
stdin,stdout,stderr=s.exec_command('free;df -h')
print stdout.read()
s.close()
执行结果如下:
total used free shared buffers cached
Mem: 20749402057420175200424161867968
-/+ buffers/cache: 1470361927904
Swap: 20964722402096232
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 30G12G17G42% /
none 1014M01014M0% /dev/shm
/dev/sda3 2.0G289M1.6G16% /var
/dev/sdb1 135G14G115G11% /data
/dev/sdc1 135G127G880M100% /data1
/dev/sdd1 135G99G30G78% /data2
2:连接远程linux主机上传下载文件(paramiko模块是用SFTP协议来实现的)
#!/usr/bin/env python
import paramiko,datetime,os
hostname='192.168.0.102'
username='root'
password='abc123'
port=22
local_dir='/tmp/'
remote_dir='/tmp/test/'
try:
t=paramiko.Transport((hostname,port))
t.connect(username=username,password=password)
sftp=paramiko.SFTPClient.from_transport(t)
#files=sftp.listdir(dir_path)
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))#下载
#sftp.put(os.path.join(local_dir,f),os.path.join(remote_dir,f))#上传
print'Download file success %s ' % datetime.datetime.now()
print''
print'##########################################'
t.close()
except Exception:
print"connect error!"
执行结果:
#########################################
Beginning to download file from192.168.0.1022012-11-0515:49:01.334686
Downloading file: /tmp/test/wgetrc Download file success 2012-11-0515:49:05.955184
##########################################
#########################################
Beginning to download file from192.168.0.1022012-11-0515:49:05.955342
Downloading file: /tmp/test/xinetd.conf Download file success 2012-11-0515:49:10.929568
##########################################
#########################################
Beginning to download file from192.168.0.1022012-11-0515:49:10.929740
Downloading file: /tmp/test/warnquota.conf Download file success 2011-12-0515:49:14.213570
##########################################
1:连接远程linux主机并执行命令
#!/usr/bin/env python
import paramiko
hostname='192.168.0.102'
username='root'
password='abc'
port=22
paramiko.util.log_to_file('paramiko.log')
s=paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname = hostname,port=port,username=username, password=password)
stdin,stdout,stderr=s.exec_command('free;df -h')
print stdout.read()
s.close()
执行结果如下:
total used free shared buffers cached
Mem: 20749402057420175200424161867968
-/+ buffers/cache: 1470361927904
Swap: 20964722402096232
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 30G12G17G42% /
none 1014M01014M0% /dev/shm
/dev/sda3 2.0G289M1.6G16% /var
/dev/sdb1 135G14G115G11% /data
/dev/sdc1 135G127G880M100% /data1
/dev/sdd1 135G99G30G78% /data2
2:连接远程linux主机上传下载文件(paramiko模块是用SFTP协议来实现的)
#!/usr/bin/env python
import paramiko,datetime,os
hostname='192.168.0.102'
username='root'
password='abc123'
port=22
local_dir='/tmp/'
remote_dir='/tmp/test/'
try:
t=paramiko.Transport((hostname,port))
t.connect(username=username,password=password)
sftp=paramiko.SFTPClient.from_transport(t)
#files=sftp.listdir(dir_path)
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))#下载
#sftp.put(os.path.join(local_dir,f),os.path.join(remote_dir,f))#上传
print'Download file success %s ' % datetime.datetime.now()
print''
print'##########################################'
t.close()
except Exception:
print"connect error!"
执行结果:
#########################################
Beginning to download file from192.168.0.1022012-11-0515:49:01.334686
Downloading file: /tmp/test/wgetrc Download file success 2012-11-0515:49:05.955184
##########################################
#########################################
Beginning to download file from192.168.0.1022012-11-0515:49:05.955342
Downloading file: /tmp/test/xinetd.conf Download file success 2012-11-0515:49:10.929568
##########################################
#########################################
Beginning to download file from192.168.0.1022012-11-0515:49:10.929740
Downloading file: /tmp/test/warnquota.conf Download file success 2011-12-0515:49:14.213570
##########################################
相关文章推荐
- Searching in baidu using BeautifulSoup in Python3.3
- python paramiko模块安装和使用
- python-Django环境搭建
- pythonchallenge 解谜 Level 6
- Python 写入 Mysql 乱码问题
- python 字典列表排序operator.itemgetter()
- python函数一日一发--关于divmod函数
- python实践系列之(二)python爬取数据(上)
- python实践系列之(一)安装 python/pip/numpy/matplotlib
- pythoneer 的三大神器—fabric
- Python 简单快速折线图,绘图、报表 教程
- Python读取ini文件、操作mysql、发送邮件实例
- pythoneer 的三大神器—pip入门基础教程
- python简介-copy
- python3 requests模块安装
- selenium+python笔记10
- pythoneer 的三大神器—Virtualenv入门基础教程
- Ubuntu12.04上交叉编译python-3.2.2详细过程
- Python编写API接口
- python中生成器和迭代器