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 ...
随机推荐
- Spring autowire自动装配 ByType和ByName
不使用自动装配前使用的是类的引用: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...
- Testing - 自动化测试的几个基础概念
自动化测试框架与模型 一个自动化测试框架就是一个集成体系,在这一体系中包含测试功能的函数库.测试数据源.测试对象识别标准,以及种可重用的模块. 自动化测试框架在发展的过程中经历了几个阶段,模块驱动测试 ...
- python基础-循环语句(5)
一.循环语句介绍 一般情况下,需要多次重复执行的代码,都可以用循环的方式来完成 循环不是必须要使用的,但是为了提高代码的重复使用率,所以有经验的开发者都会采用循环 二.常见的循环形式 while循环 ...
- mysql 架构篇系列 1 复制原理和复制架构
一. 复制概述 mysql 从3.23版本开始提供复制功能,复制是指将主数据库的ddl和dml操作通过二进制日志传到复制服务器(也叫从服务器)上,然后在从服务器上对这些日志重新执行(也叫重做),从而使 ...
- HDU 1006 Digital Roots
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- 7-Flink的分布式缓存
分布式缓存 Flink提供了一个分布式缓存,类似于hadoop,可以使用户在并行函数中很方便的读取本地文件,并把它放在taskmanager节点中,防止task重复拉取. 此缓存的工作机制如下:程序注 ...
- python练习四—简单的聊天软件
python最强大的是什么?库支持!!有了强大的库支持,一个简单的聊天软件实现就更简单了,本项目思路如下 # 项目思路 1. 服务器的工作 * 初始化服务器 * 新建一个聊天房间 * 维护一个已链接用 ...
- Angular2入门:TypeScript的接口
- Go Web:Cookie
Cookie用来解决http协议无状态的问题. 首先,在服务端生成Cookie,然后在http响应header中设置Set-Cookie字段,客户端会读取到Set-Cookie字段后,会将cookie ...
- 基于.Net进行前端开发的技术栈发展路线(一)
前言 今天想讲讲的是我的技术树.我最初是做CS开发的,第一阶段的技术经历是以Powerbuilder来做CS开发,第二阶段开始基于C#做winform开发,眼看前端开发越来越流行,需要更广泛的技术栈势 ...

