第三方模块paramiko的使用
"Paramiko" is a combination of the Esperanto words for "paranoid" and "friend". It's a module for Python 2.7/3.4+ that implements the SSH2 protocol for secure (encrypted and authenticated) connections to remote machines. Unlike SSL (aka TLS), SSH2 protocol does not require hierarchical certificates signed by a powerful central authority. You may know SSH2 as the protocol that replaced Telnet and rsh for secure access to remote shells, but the protocol also includes the ability to open arbitrary channels to remote services across the encrypted tunnel (this is how SFTP works, for example).
paramiko是一个远程控制模块,使用它可以很容易的再python中使用SSH执行命令和使用SFTP上传或下载文件;而且paramiko直接与远程系统交互,无需编写服务端。
例一(实现一个简单的SSH客户端):
import paramiko #实例化一个ssh客户端实例
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#连接远程 --- 填入要连接的地址,端口,用户,密码
ssh.connect(hostname="192.168.56.50", port=22, username='libin', password='') while True:
command = input(">>>:") #exec_command()返回三个值:the stdin, stdout, and stderr of the executing command, as a 3-tuple
stdin, stdout, stderr = ssh.exec_command(command) result = stdout.read()
error = stderr.read() if result:
print(result.decode())
else:
print(error.decode())
#关闭连接
ssh.close()
ssh_client
例二(实现文件的上传和下载操作):
import paramiko
transport = paramiko.Transport('192.168.56.50', 22)
transport.connect(username='libin', password='')
#实例化一个SFTP客户端实例
sftp = paramiko.SFTPClient.from_transport(transport)
#put('localpath', 'remotepath')上传本地文件至服务器
#sftp.put(r'E:\tempdownload\Sau_Authentication_Client_For_Windows_V6.82.exe', '/tmp/abc.exe')
#get('remotepath', 'localpath')将远程文件下载至本地
sftp.get('/tmp/abc.exe', r'E:\tempdownload\abc.exe')
transport.close()
sftp_client
为了连接安全,我们可以对上述两例稍加改变,使其使用密钥认证建立连接。
例四(以密钥认证实现SSH):
import paramiko #实例化一个ssh客户端实例
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#连接远程 --- 填入要连接的地址,端口,用户,密码 #导入私钥文件
private_key = paramiko.RSAKey.from_private_key_file('id_rsa_2048')
#连接远程
ssh.connect(hostname="192.168.56.50", port=22, username='libin', pkey=private_key) #无需再填入用户密码 while True:
command = input(">>>:") #exec_command()返回三个值:the stdin, stdout, and stderr of the executing command, as a 3-tuple
stdin, stdout, stderr = ssh.exec_command(command) result = stdout.read()
error = stderr.read() if result:
print(result.decode())
else:
print(error.decode())
#关闭连接
ssh.close()
key_ssh_client
例五(以密钥认证实现SFTP):
import paramiko
transport = paramiko.Transport('192.168.56.50', 22)
#导入私钥文件
privat_key = paramiko.RSAKey.from_private_key_file('id_rsa_2048')
transport.connect(username='libin', pkey=privat_key)
#实例化一个SFTP客户端实例
sftp = paramiko.SFTPClient.from_transport(transport)
#put('localpath', 'remotepath')上传本地文件至服务器
sftp.put(r'E:\tempdownload\test.exe', '/tmp/123.exe')
# # get('remotepath', 'localpath')将远程文件下载至本地
# sftp.get('/tmp/abc.exe', r'E:\tempdownload\abc.exe')
transport.close()
key_sftp_client
第三方模块paramiko的使用的更多相关文章
- python第三方模块精选
python不但有着强大丰富的“内置电池”,同样的,第三方模块也是非常的多.目前收集了requests.paramiko.pymsql,以后会陆续添加: 一.requests Python标准库中提供 ...
- python 常用第三方模块
除了内建的模块外,Python还有大量的第三方模块. 基本上,所有的第三方模块都会在https://pypi.python.org/pypi上注册,只要找到对应的模块名字,即可用pip安装. 本章介绍 ...
- 【Python】[模块]使用模块,安装第三方模块
一个.py文件就称之为一个模块(Model)按目录来组织模块的方法,称为包(Package)每一个包目录下面都会有一个__init__.py的文件内置函数1.使用模块 导入模块 import sys ...
- 安装第三方模块方法和requests
如何安装第三方模块 pip3 pip3 install xxxx 源码 下载,解压 进入目录 python setup.py inst ...
- Python:Pycharm下无法导入安装好的第三方模块?
Pycharm下无法导入安装好的第三方模块requests? 在cmd下使用pip安装好requests模块后,可以使用import requests,但在Pycharm IDE下无法导入,出现如下错 ...
- python 使用pip安装第三方模块
part 1:使用方法: 1.pip install somePackage picture 1 2.pip show somePackage 例如:pip show pip 弹出关于该模块的信息 p ...
- SAE上安装第三方模块
当sae上没有自己所需要的第三方模块时,可以使用saecloud install package [package...]将所需要的模块安装到本地应用文件夹下,然后在index.wsgi下添加如何代码 ...
- python基础——第三方模块
python基础——第三方模块 在Python中,安装第三方模块,是通过包管理工具pip完成的. 如果你正在使用Mac或Linux,安装pip本身这个步骤就可以跳过了. 如果你正在使用Window ...
- Python-Windows下安装BeautifulSoup和requests第三方模块
http://blog.csdn.net/yannanxiu/article/details/50432498 首先给出官网地址: 1.Request官网 2.BeautifulSoup官网 我下载的 ...
随机推荐
- 线程池内的异步线程创建UI控件,造成UI线程卡死无响应的问题分析
winform应用在使用一段时间后,切换到其他系统或者打开word.excel文档,再切换回winform应用时,系统有时出现不响应的现象.有时在锁屏后恢复桌面及应用时也发生此问题. 经微软支持确认, ...
- PIL遇到问题解决
PIL 全称:Pillow 在使用PIL4.2.1版本读取jpeg文件时,报cannot identify image file,去github源查找原因:https://github.com/pyt ...
- spring boot +mysql + mybatis + druid的整理(一)——单数据源
一,使用spring boot脚手架搭建spring boot框架生成maven项目 如下图所示: 设置自定义的坐标,即左侧的Group和Artifact,右侧可以搜索添加一些依赖,搜索不到的可以在p ...
- mongodb 聚合查询
操作符介绍: $project:包含.排除.重命名和显示字段 $match:查询,需要同find()一样的参数 $limit:限制结果数量 $skip:忽略结果的数量 $sort:按照给定的字段排序结 ...
- “玲珑杯”ACM比赛 Round #13 题解&源码
A 题目链接:http://www.ifrog.cc/acm/problem/1111 分析:容易发现本题就是排序不等式, 将A数组与B数组分别排序之后, 答案即N∑i=1Ai×Bi 此题有坑,反正据 ...
- hdu_1370Biorhythms(互素的中国剩余定理)
Biorhythms Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 南阳理工oj_The Triangle
The Triangle 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 ...
- android文件选择器、仿淘宝编辑页面、新手引导层等源码
Android精选源码 单片机和安卓应用,传感器 文件选择器 android滑动选择的尺子view源码 android视频录制 视频压缩的源码 仿今日头条顶部导航指示器源码 Android框架+常用控 ...
- [UWP]使用Reveal
1. 前言 之前在 如何使用Fluent Design System 这篇文章里已经简单介绍过Reveal的用法,这篇再详细介绍其它内容. 2. 自定义RevealButtonStyle 我觉得常用I ...
- CSS鼠标样式 cursor 属性
值 描述 url 需使用的自定义光标的 URL. 注释:请在此列表的末端始终定义一种普通的光标,以防没有由 URL 定义的可用光标. default 默认光标(通常是一个箭头) auto 默认.浏览器 ...