第三方模块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官网 我下载的 ...
随机推荐
- \(?0\d{2}[) -]?\d{8}正则表达式的解释
正则表达式30分钟出现了这样一个正则表达式:(?0\d{2}[) -]?\d{8} "("和")"也是元字符,所以需要转义. ?匹配零或一次 (? 表示 出现( ...
- viewport预备知识
dpr === dppx dpr:device pixel ratio 设备像素比 dppx:Number of dots per px unit 每像素有多少点 . 1dppx = 96dpi dp ...
- 常用SQL语句集合
一.数据定义 1.创建新数据库:CREATE DATABASE database_name2.创建新表:CREATE TABLE table_name (column_name datatype,co ...
- vs2012 .net4.0 nuget 导入NHibernate4.0版本
问题描述: 最近弄一个项目,打算使用NHibernate,本人使用的VS2012,项目用的是.NET 4.0.在使用Nuget安装引用的时候,发现安装失败,提示如下图: 意思是当前安装的NHibern ...
- 查找算法的实现(C/C++实现)
存档: #include <stdio.h> #include <stdlib.h> #define max 20 typedef int keytype; #include ...
- CTF---Web入门第十三题 拐弯抹角
拐弯抹角分值:10 来源: cwk32 难度:易 参与人数:5765人 Get Flag:2089人 答题人数:2143人 解题通过率:97% 如何欺骗服务器,才能拿到Flag? 格式:CTF{} 解 ...
- SpringMVC框架学习笔记(2)——使用注解开发SpringMVC
1.配置web.xml <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.sp ...
- oracle存储过程的创建和使用
创建存储过程: 格式:create or replace procedure procedure_name(参数 参数类型) Is/as 变量1 变量1的类型: begin ----------业务逻 ...
- 自写 zTree搜索功能 -- 关键字查询 -- 递归无限层
唠叨一哈 前两天朋友跟我说要一个ztree的搜索功能,我劈头就是一巴掌:这种方法难道无数前辈还做少了?自己去找,我很忙~然后我默默地蹲着写zTree的搜索方法去了.为什么呢?因为我说了句“找不到是不可 ...
- CMD命令操作MySql数据库详解
第一:mysql服务的启动和停止 1. net stop mysql 2. net start mysql 第二:登录 mysql –u用户名 [–h主机名或者IP地址] –p密码 例如:mysq ...