第三方模块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官网 我下载的 ...
随机推荐
- 基于Flask实现博客开发--准备工作
背景说明 本项目是基于<深入理解flask>一书,主要是用来记录学习历程和交流心得,所以写得不好请大神勿喷. 准备工作 virtualenv介绍 也许 Virtualenv 是你在开发中最 ...
- 用LSTM分类 MNIST
LSTM是RNN的一种算法, 在序列分类中比较有用.常用于语音识别,文字处理(NLP)等领域. 等同于VGG等CNN模型在在图像识别领域的位置. 本篇文章是叙述LSTM 在MNIST 手写图中的使用 ...
- Java与算法之(8) - 堆排序
堆是一种特殊的完全二叉树,其特点是所有父节点都比子节点要小,或者所有父节点都比字节点要大.前一种称为最小堆,后一种称为最大堆. 比如下面这两个: 那么这个特性有什么作用?既然题目是堆排序,那么肯定能用 ...
- 从头开始基于Maven搭建SpringMVC+Mybatis项目(3)
接上文内容,本节介绍基于Mybatis的查询和分页功能,并展示一个自定义的分页标签,可重复使用以简化JSP页面的开发. 从头阅读传送门 在上一节中,我们已经使用Maven搭建好了项目的基础结构,包括一 ...
- 2039: [2009国家集训队]employ人员雇佣
任意门 Description 作为一个富有经营头脑的富翁,小L决定从本国最优秀的经理中雇佣一些来经营自己的公司.这些经理相互之间合作有一个贡献指数,(我们用Ei,j表示i经理对j经理的了解程度),即 ...
- 我的第三个网页制作:b、i、s、u、sub、sup标签的使用
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- [bzoj2462] [BeiJing2011]矩阵模板
二维的hash.. 注意n的范围是1000........ 真相似乎是全部输出1就行了233 #include<cstdio> #include<iostream> #incl ...
- angular $cookies、$cookieStore
js 文件 加载 <script src="angular-cookies/angular-cookies.js"></script>注入:angular. ...
- cesium编程入门(四)界面介绍及小控件隐藏
感性认识 界面介绍,viewer Geocoder : 查找位置工具,查找到之后会将镜头对准找到的地址,默认使用bing地图 Home Button :视角返回初始位置. Scene Mode Pic ...
- 使用phpMyAdmin批量修改Mysql数据表前缀的方法
多个网站共用一个Mysql数据库时,为使数据库管理不混乱,一般采用不同的网站使用不同前缀名的方式进行区分.而如何批量修改已有数据库的前缀名呢?全部导出修改后再导入?还是一个表一个表的修改?今天我要介绍 ...