第三方模块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官网 我下载的 ...
随机推荐
- Zabbix实战-简易教程--动作(Actions)--自动注册
一.概述 之前已经讲述了自动发现功能,自动注册和自动发现非常类似,但是比自动发现更精确.因为自动注册,是在Agent上自定义元数据,然后Agent将元数据发送给server进行匹配,如果匹配一致,则进 ...
- Smart-image通过SoftReference提高性能
文章导读: 文件介绍了常见的图片下载开源插件smart-image, 由于移动设备硬件受限,因此Android的相关app都要考虑到性能的关系, 所以很多的第三方插件都使用到了缓存cache技术,本人 ...
- python模块安装
现在终于知道怎么在windows上导入Python的第三方模块了 首先在DOS下进入Python安装的pip目录:D:/Python27/Scripts 用pip install XXX安装 之前一直 ...
- Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】
D. Time to go back time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- MySQL的ibdata1文件占用过大
处理MySQL的ibdata1文件过大问题 本人遇到一次在安装zabbix监控的时候,yum安装的MySQL数据库,后面用了一段时间发现data目录下的ibdata1的空间特别大,反而我的zabbix ...
- 学习Spring必学的Java基础知识(1)----反射
引述要学习Spring框架的技术内幕,必须事先掌握一些基本的Java知识,正所谓"登高必自卑,涉远必自迩".以下几项Java知识和Spring框架息息相关,不可不学(我将通过一个系 ...
- 分布式计算框架学习笔记--hadoop工作原理
(hadoop安装方法:http://blog.csdn.net/wangjia55/article/details/53160679这里不再累述) hadoop是针对大数据设计的一个计算架构.如果你 ...
- LuceneNet 实现快速大文件大数据查询
做过站内搜索的朋友应该对Lucene.Net不陌生,因为用普通的sql like查询肯定是不行的,太慢了. 首先说明的是--Lucene.Net只是一个全文检索开发包,不是一个成型的搜索引擎, 它的 ...
- Spring的IOC分析(二)源码
承接上节继续,分析Ioc的工作原理,在典型的 IOC 场景中,容器创建了所有对象,并设置必要的属性将它们连接在一起(同时一个叫DI"依赖注入"或DL"依赖查找" ...
- :nth-child(n)
规定属于其父元素的第二个子元素的每个 p 的背景色: p:nth-child(2) { background:#ff0000; } 1定义和用法 :nth-child(n) 选择器匹配属于其父元素的第 ...