python paramiko的使用介绍
一: 使用paramiko
#设置ssh连接的远程主机地址和端口
t=paramiko.Transport((ip,port))
#设置登录名和密码
t.connect(username=username,password=password)
#连接成功后打开一个channel
chan=t.open_session()
#设置会话超时时间
chan.settimeout(session_timeout)
#打开远程的terminal
chan.get_pty()
#激活terminal
chan.invoke_shell()
然后就可以通过chan.send('command')和chan.recv(recv_buffer)来远程执行命令以及本地获取反馈。
二: paramiko的两个模块介绍
paramiko有两个模块SSHClient()和SFTPClient()
SSHClient()的使用代码:
import paramiko
ssh = paramiko.SSHClient() # 创建SSH对象
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='192.168.2.103', port=22, username='root', password='123456')
stdin, stdout, stderr = ssh.exec_command('ls') # 执行命令
result = stdout.read() # 获取命令结果
print (str(result,encoding='utf-8'))
ssh.close() # 关闭连接
SSHClient()里有个transport变量,是用于获取连接,我们也可单独的获取到transport变量,然后执行连接操作
import paramiko
transport = paramiko.Transport(('192.168.2.103', 22))
transport.connect(username='root', password='123456')
ssh = paramiko.SSHClient()
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command('df')
print (str(stdout.read(),encoding='utf-8'))
transport.close()
用transport实现上传下载以及命令的执行:
#coding:utf-8
import paramiko
import uuid
class SSHConnection(object):
def __init__(self, host='192.168.2.103', port=22, username='root',pwd='123456'):
self.host = host
self.port = port
self.username = username
self.pwd = pwd
self.__k = None
def connect(self):
transport = paramiko.Transport((self.host,self.port))
transport.connect(username=self.username,password=self.pwd)
self.__transport = transport
def close(self):
self.__transport.close()
def upload(self,local_path,target_path):
# 连接,上传
# file_name = self.create_file()
sftp = paramiko.SFTPClient.from_transport(self.__transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put(local_path, target_path)
def download(self,remote_path,local_path):
sftp = paramiko.SFTPClient.from_transport(self.__transport)
sftp.get(remote_path,local_path)
def cmd(self, command):
ssh = paramiko.SSHClient()
ssh._transport = self.__transport
# 执行命令
stdin, stdout, stderr = ssh.exec_command(command)
# 获取命令结果
result = stdout.read()
print (str(result,encoding='utf-8'))
return result
ssh = SSHConnection()
ssh.connect()
ssh.cmd("ls")
ssh.upload('s1.py','/tmp/ks77.py')
ssh.download('/tmp/test.py','kkkk',)
ssh.cmd("df")
ssh.close()

1 #coding:utf-8
2 import paramiko
3 import uuid
4
5 class SSHConnection(object):
6
7 def __init__(self, host='192.168.2.103', port=22, username='root',pwd='123456'):
8 self.host = host
9 self.port = port
10 self.username = username
11 self.pwd = pwd
12 self.__k = None
13
14 def connect(self):
15 transport = paramiko.Transport((self.host,self.port))
16 transport.connect(username=self.username,password=self.pwd)
17 self.__transport = transport
18
19 def close(self):
20 self.__transport.close()
21
22 def upload(self,local_path,target_path):
23 # 连接,上传
24 # file_name = self.create_file()
25 sftp = paramiko.SFTPClient.from_transport(self.__transport)
26 # 将location.py 上传至服务器 /tmp/test.py
27 sftp.put(local_path, target_path)
28
29 def download(self,remote_path,local_path):
30 sftp = paramiko.SFTPClient.from_transport(self.__transport)
31 sftp.get(remote_path,local_path)
32
33 def cmd(self, command):
34 ssh = paramiko.SSHClient()
35 ssh._transport = self.__transport
36 # 执行命令
37 stdin, stdout, stderr = ssh.exec_command(command)
38 # 获取命令结果
39 result = stdout.read()
40 print (str(result,encoding='utf-8'))
41 return result
42
43 ssh = SSHConnection()
44 ssh.connect()
45 ssh.cmd("ls")
46 ssh.upload('s1.py','/tmp/ks77.py')
47 ssh.download('/tmp/test.py','kkkk',)
48 ssh.cmd("df")
49 ssh.close()

python paramiko的使用介绍的更多相关文章
- python paramiko 模块简单介绍
背景,公司的很多服务包括数据库访问都需要通过跳板机访问,为日常工作及使用带来了麻烦,特别数python直接操作数据更是麻烦了,所以一直想实现python 通过跳板机访问数据库的操作. 首先了解到了 p ...
- python+paramiko库+svn写的自动化部署脚本
第一篇博文 直接开门见山的说了. 这是件什么事?:每次部署都是复制本地的文件粘贴到服务器端,因为路径复杂,所以费时且手工容易出漏洞. 一直在想有什么办法可以解决这种,因为以前在微软的一个牛人同事做过一 ...
- python Scrapy安装和介绍
python Scrapy安装和介绍 Windows7下安装1.执行easy_install Scrapy Centos6.5下安装 1.库文件安装yum install libxslt-devel ...
- Python Paramiko模块与MySQL数据库操作
Paramiko模块批量管理:通过调用ssh协议进行远程机器的批量命令执行. 要使用paramiko模块那就必须先安装这个第三方模块,仅需要在本地上安装相应的软件(python以及PyCrypto), ...
- python paramiko模拟ssh登录,实现sftp上传或者下载文件
Python Paramiko模块的安装与使用详解 paramiko是短链接,不是持续链接,只能执行你设定的shell命令,可以加分号执行两次命令. http://www.111cn.net/phpe ...
- [翻译]Mock 在 Python 中的使用介绍
目录 Mock 在 Python 中的使用介绍 原文链接与说明 恐惧系统调用 一个简单的删除函数 使用 Mock 重构 潜在陷阱 向 'rm' 中加入验证 将文件删除作为服务 方法 1:模拟实例的方法 ...
- Python的基本类型介绍和可变不可变
Python的基本类型介绍 前言 做python有一段时间了,从工作开始就在不断地学习和积累.但是有时候用到一些技术点,甚至是基础知识的时候,总是会遗忘.所以,从今天开始,就在这里记录下来,不仅可以分 ...
- Python 爬虫利器 Selenium 介绍
Python 爬虫利器 Selenium 介绍 转 https://mp.weixin.qq.com/s/YJGjZkUejEos_yJ1ukp5kw 前面几节,我们学习了用 requests 构造页 ...
- Python 基于python操纵redis入门介绍
基于python操纵redis入门介绍 by:授客 QQ:1033553122 测试环境 redis-3.0.7 CentOS 6.5-x86_64 python 3.3.2 基于Python操作R ...
随机推荐
- Eclipse JAX-RS (REST Web Services) 2.0 requires Java 1.6 or newer
pom.xml文件中添加: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins&l ...
- Beta冲刺(5/7)——2019.5.27
作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Beta冲刺(团队) 团队目标 切实可行的计算机协会维修预约平台 开发工具 Eclipse 团队信息 队员学号 队 ...
- MySQL事务未提交导致整个表锁死
问题及说明: 当一个SQL事务执行完了,但未COMMIT,后面的SQL想要执行就是被锁,超时结束:报错信息如下: mysql> ERROR 1205 (HY000): Lock wait tim ...
- CSS3幸运大转盘最简单的写法
点击开始 直接css动画 如果你要自己控制转到哪里 那就多写几个class 根据不同角度 运行不同的class..<pre>.zhuandong{ animation: zhuandong ...
- C++:const
const const是C++提供的一个强大的关键字,const的用法非常多,但总的来说,const的作用只有一个:保证被修饰的内容不会被程序所修改. const基本用法 对一个类型的对象使用cons ...
- Redis学习之intset整数集合源码分析
1.整数集合:整数的集合,升序排序,无重复元素 2.整数集合intset是集合键的底层实现之一,当一个集合只包含整数值的元素,并且这个集合的元素数量不多时,redis会使用整数集合作为集合键的底层实现 ...
- 第七节:EF Core调用SQL语句和存储过程
一. 查询类(FromSql) 1.说明 A. SQL查询必须返回实体的所有属性字段. B. 结果集中的列名必须与属性映射到的列名相匹配. C. SQL查询不能包含关联数据 D. 除Select以为的 ...
- Prometheus 运维监控
Prometheus 运维监控 1.Prometheus 介绍详解 2.Prometheus 安装部署 3.Prometheus 配置文件详解 4.Prometheus PromSQL 常用资源 5. ...
- 科xue上网工具
原来这样严格了吗 收藏一个工具列表: http://next.36kr.com/posts/collections/75
- 1 Python命令行参数(脚本神器)
#!/usr/bin/env python3.7 # -*- coding:utf-8 -*- # Author: Lancer 2019-09-02 10:07:21 import sys,geto ...