Python 基于Python实现的ssh兼sftp客户端(上)
基于Python实现的ssh兼sftp客户端
by:授客 QQ:1033553122
实现功能
实现ssh客户端兼ftp客户端:实现远程连接,执行linux命令,上传下载文件
测试环境
Win7 64位
Python 3.3.4
paramiko 1.15.2
下载地址:
https://pypi.python.org/pypi/paramiko/1.15.2
https://pan.baidu.com/s/1i4SJ1CL
cryptography-1.0-cp34-none-win_amd64.whl
(如果paramiko可以正常安装完,则不需要安装该类库)
下载地址:
https://pypi.python.org/pypi/cryptography/1.0
https://pan.baidu.com/s/1jIRBJvg
安装好后,找到nt.py(本例中路径为:
Lib\site-packages\pycrypto-2.6.1-py3.4-win-amd64.egg\Crypto\Random\OSRNG\nt.py),修改
import winrandom
为
from Crypto.Random.OSRNG import winrandom
如下
#import winrandom
from Crypto.Random.OSRNG import winrandom
以解决ImportError: No module named 'winrandom'错误
VS2010
因操作系统而异,可能需要安装VS2010,以解决包依赖问题
代码实践
mysshclient.py
#!/usr/bin/env/ python
#
-*- coding:utf-8 -*-
__author__
=
'shouke'
import
os
from
paramiko.client
import
AutoAddPolicy
from
paramiko.client
import
SSHClient
from
otherTools
import
OtherTools
class
MySSHClient:
def
__init__(self):
self.ssh_client
= SSHClient()
#
连接登录
def
connect(self,
hostname, port, username, password):
try:
print('正在远程连接主机:%s'
% hostname)
self.ssh_client.set_missing_host_key_policy(AutoAddPolicy())
self.ssh_client.connect(hostname=hostname,
port=port,
username=username,
password=password)
return
[True,
'']
except
Exception
as
e:
print('连接出错了%s'
% e)
return
[False,
'%s'
% e]
#
远程执行命令
def
exec_command(self,
command):
try:
print('正在执行命令:'+
command)
stdin,
stdout, stderr =
self.ssh_client.exec_command(command)
print('命令输出:')
print(stdout.read())
# 读取命令输出
return
[True,
tuple]
except
Exception
as
e:
print('执行命:%s令出错'
% command)
return
[False,'%s'
% e]
#
下载文件(非目录文件)
def
download_file(self,
remotepath, localpath):
try:
localpath
= os.path.abspath(localpath)
localpath
= localpath.replace('\t',
'/t').replace('\n',
'/n').replace('\r',
'/r').replace('\b',
'/b')
# 转换特殊字符
localpath
= localpath.replace('\f',
'/f')
print('转换后的本地目标路径为:%s'
% localpath)
head,
tail = os.path.split(localpath)
if
not
tail:
print('下载文件:%s
到本地:%s失败,本地文件名不能为空'
% (remotepath, localpath))
return
[False,
'下载文件:%s
到本地:%s失败,本地文件名不能为空'
% (remotepath, localpath)]
if
not
os.path.exists(head):
print('本地路径:%s不存在,正在创建目录'
% head)
OtherTools().mkdirs_once_many(head)
sftp_client
=
self.ssh_client.open_sftp()
print('正在下载远程文件:%s
到本地:%s'
% (remotepath, localpath))
sftp_client.get(remotepath,
localpath)
sftp_client.close()
return
[True,
'']
except
Exception
as
e:
print('下载文件:%s
到本地:%s
出错:%s'
% (remotepath, localpath, e))
return
[False,
'下载文件:%s
到本地:%s
出错:%s'
% (remotepath, localpath, e)]
#
上传文件(非目录文件)
def
upload_file(self,
localpath, remotepath):
try:
localpath
= localpath.rstrip('\\').rstrip('/')
localpath
= localpath.replace('\t',
'/t').replace('\n',
'/n').replace('\r',
'/r').replace('\b',
'/b')
# 转换特殊字符
localpath
= localpath.replace('\f',
'/f')
localpath
= os.path.abspath(localpath)
print('转换后的本地文件路径为:%s'
% localpath)
remotepath
= remotepath.rstrip('\\').rstrip('/')
head,
tail = os.path.split(localpath)
if
not
tail:
print('上传文件:%s
到远程:%s失败,本地文件名不能为空'
% (localpath, remotepath))
return
[False,
'上传文件:%s
到远程:%s失败,本地文件名不能为空'
% (localpath, remotepath)]
if
not
os.path.exists(head):
print(
'上传文件:%s
到远程:%s失败,父路径不存在'
% (localpath, remotepath, head))
return
[False,
'上传文件:%s
到远程:%s失败,父路径不存在'
% (localpath, remotepath, head)]
if
not
(remotepath.startswith('/')
or
remotepath.startswith('.')):
print('上传文件:%s
到远程:%s失败,远程路径填写不规范%s'
% (localpath, remotepath,remotepath))
return
[False,
'上传文件:%s
到远程:%s失败,远程路径填写不规范%s'
% (localpath, remotepath,remotepath)]
sftp_client
=
self.ssh_client.open_sftp()
head,
tail = os.path.split(remotepath)
head
= sftp_client.normalize(head)
# 规范化路径
remotepath
= head +
'/'
+ tail
print('规范化后的远程目标路径:',
remotepath)
print('正在上传文件:%s
到远程:%s'
% (localpath, remotepath))
sftp_client.put(localpath,
remotepath)
sftp_client.close()
return
[True,
'']
except
Exception
as
e:
print('上传文件:%s
到远程:%s
出错:%s'
% (localpath, remotepath, e))
return
[False,
'上传文件:%s
到远程:%s
出错:%s'
% (localpath, remotepath, e)]
def
close(self):
self.ssh_client.close()
Python 基于Python实现的ssh兼sftp客户端(下)
Python 基于Python实现的ssh兼sftp客户端(上)的更多相关文章
- Python 基于Python实现的ssh兼sftp客户端(下)
基于Python实现的ssh兼sftp客户端 by:授客 QQ:1033553122 otherTools.py #!/usr/bin/env/ python # -*- coding:utf-8 ...
- Python 基于Python及zookeeper实现简单分布式任务调度系统设计思路及核心代码实现
基于Python及zookeeper实现简单分布式任务调度系统设计思路及核心代码实现 by:授客 QQ:1033553122 测试环境 功能需求 实现思路 代码实践(关键技术点实现) 代码模块组织 ...
- Python 基于python操纵zookeeper介绍
基于python操纵zookeeper介绍 by:授客 QQ:1033553122 测试环境 Win7 64位 Python 3.3.4 kazoo-2.6.1-py2.py3-none-any.w ...
- Python 基于Python结合pykafka实现kafka生产及消费速率&主题分区偏移实时监控
基于Python结合pykafka实现kafka生产及消费速率&主题分区偏移实时监控 By: 授客 QQ:1033553122 1.测试环境 python 3.4 zookeeper- ...
- Python 基于Python从mysql表读取千万数据实践
基于Python 从mysql表读取千万数据实践 by:授客 QQ:1033553122 场景: 有以下两个表,两者都有一个表字段,名为waybill_no,我们需要从tl_waybill_b ...
- Python基于Python实现批量上传文件或目录到不同的Linux服务器
基于Python实现批量上传文件或目录到不同的Linux服务器 by:授客 QQ:1033553122 实现功能 1 测试环境 1 使用方法 1 1. 编辑配置文件conf/rootpath_fo ...
- Python 基于python编写一些算法程序等
基于python编写一些算法程序等 by:授客 QQ:1033553122 QQ群:7156436 没特意去研究,只是这对群友在QQ群里(7156436)提出的一些小程序实现.编程题,算法.问题等,本 ...
- Python 基于python+mysql浅谈redis缓存设计与数据库关联数据处理
基于python+mysql浅谈redis缓存设计与数据库关联数据处理 by:授客 QQ:1033553122 测试环境 redis-3.0.7 CentOS 6.5-x86_64 python 3 ...
- Python 基于python操纵redis入门介绍
基于python操纵redis入门介绍 by:授客 QQ:1033553122 测试环境 redis-3.0.7 CentOS 6.5-x86_64 python 3.3.2 基于Python操作R ...
随机推荐
- PythonDay02——编程语言、python介绍以及安装解释器、运行程序的两种方式、变量
一.编程语言 1.1 机器语言:直接用计算机能理解的二进制指令编写程序,直接控制硬件 1.2 汇编语言:用英文标签取代二进制指令去编写程序,本质也是直接控制硬件 1.3 高级语言:用人能理解的表达方式 ...
- npm jspdf报错
137073 warn optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.2 (node_modules\chokidar\node_modul ...
- 使用maven插件构建docker镜像
为什么要用插件 主要还是自动化的考虑,如果额外使用Dockerfile进行镜像生成,可能会需要自己手动指定jar/war位置,并且打包和生成镜像间不同步,带来很多琐碎的工作. 插件选择 使用比较多的是 ...
- Linux编程 19 编辑器(vim 用法)
一.概述 在开启shell脚本编程之前,必须要知道一款文本编辑器的用法,如文本编辑的查找,剪切,粘贴,定位等, 本篇只讲vim编辑器.vim编辑器全名叫vi improved,是经过对Unix系统vi ...
- bash 的配置文件加载顺序
bash配置文件的加载顺序和登陆方式有关,下面先介绍下登陆方式. 1 登陆方式有2种 登陆式SHELL: su - oracle su -l oracle 正常从终端登陆 非登录式SHELL: ...
- Ansible批量更新远程主机用户密码 (包括Ansible批量做ssh互信)
按照集团运维信息安全制度, 需要每个一段时间对线上服务器密码进行一次变更,通过shell脚本部署比较繁琐,所以决定采用ansible脚本对远程主机root密码进行批量重置,该脚本已经在稳定运行在正式环 ...
- 计算n的阶乘有多少个尾随零
思路一: 计算出n!= nValue,然后 nValue % 10 == 0 则nCount自增1,nValue /= 10 直到条件为否,最后nCount就是我们想要的结果,代码如下: int Co ...
- openssl dhparam(密钥交换)
openssl系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html openssl dhparam用于生成和管理dh文件.dh(Diffie-H ...
- Perl回调函数和闭包
在Perl中,子程序的引用常用来做回调函数(callback).闭包(closure),特别是匿名子程序. 回调函数(callback) 关于什么是回调函数,见一文搞懂:词法作用域.动态作用域.回调函 ...
- oracle10G/11G官方迅雷下载地址合集
原文链接:https://blog.csdn.net/zlsunnan/article/details/8058235 ORACLE11GR2 Oracle Database 11g Release ...

