python3之paramiko模块
1、paramiko模块介绍
paramiko模块提供了基于ssh连接,进行远程登录服务器执行命令和上传下载文件的功能。这是一个第三方的软件包,使用之前需要安装。
2、paramiko的使用方法
(1)基于用户名和密码的sshclient方式登陆
#!/usr/bin/env python
#coding:utf8 import paramiko
#创建sshclient对象
ssh = paramiko.SSHClient()
#允许将信任的主机自动加入到host_allow 列表,此方法必须放在connect方法的前面
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#调用connect方法连接服务器
ssh.connect(hostname='172.16.32.129',port=2323,username='root',password='123.com')
while True:
input_command = input('>>>:')
if input_command == 'quit':
break
#执行命令,输出结果在stdout中,如果是错误则放在stderr中
stdin,stdout,stderr = ssh.exec_command(input_command)
result = stdout.read() #read方法读取输出结果
if len(result) == 0: #判断如果输出结果长度等于0表示为错误输出
print(stderr.read())
else:
print(str(result,'utf-8'))
ssh.close()
封装方法,隐藏属性:
#config.ini文件
[ssh]
host=172.16.32.129
port=2323
user=root
pwd=123.com
timeout=1.1 #封装ssh类
#!/usr/bin/env python
#coding:utf8
import configparser,paramiko
class parmikoclient(object):
def __init__(self,ini_file):
self.config=configparser.ConfigParser()
self.config.read(ini_file)
self.host = self.config.get('ssh','host')
self.port = self.config.get('ssh', 'port')
self.user = self.config.get('ssh', 'user')
self.pwd = self.config.get('ssh', 'pwd')
self.timeout = self.config.get('ssh', 'timeout')
self.client=paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(hostname=self.host,port=self.port,username=self.user,password=self.pwd,timeout=float(self.timeout))
def run_ssh(self,cmd_command):
# 执行命令,输出结果在stdout中,如果是错误则放在stderr中
stdin,stdout,stderr = self.client.exec_command(cmd_command)
result = stdout.read() # read方法读取输出结果
if len(result) == 0: # 判断如果输出结果长度等于0表示为错误输出
print(stderr.read().decode())
else:
print(str(result, 'utf-8'))
def close(self):
self.client.close()
if __name__ == '__main__':
client_cmd = parmikoclient('config.ini')
while True:
cmd_input=input('>>>:')
client_cmd.run_ssh(cmd_input)
if cmd_input == 'quit':
client_cmd.close()
通过transport方式登录:
#!/usr/bin/env python
#coding:utf8
import paramiko
#实例化一个transport对象
transport = paramiko.Transport(('172.16.32.129',2323))
#建立连接
transport.connect(username='root',password='')
#建立ssh对象
ssh = paramiko.SSHClient()
#绑定transport到ssh对象
ssh._transport=transport
#执行命令
stdin,stdout,stderr=ssh.exec_command('df')
#打印输出
print(stdout.read().decode())
#关闭连接
transport.close()
(2)基于密钥的sshclient方式登陆
#!/usr/bin/env python
#coding:utf8
#必须先将公钥文件传输到服务器的~/.ssh/authorized_keys中
import paramiko
# 指定本地的RSA私钥文件,如果建立密钥对时设置的有密码,password为设定的密码,如无不用指定password参数
pkey = paramiko.RSAKey.from_private_key_file('id_rsa_1024') #建立连接
ssh = paramiko.SSHClient()
#允许将信任的主机自动加入到known_hosts列表
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='172.16.32.129',port=2323,username='root',pkey=pkey) #指定密钥连接
#执行命令
stdin,stdout,stderr=ssh.exec_command('free -m')
print(stdout.read().decode())
ssh.close()
以上需要确保被访问的服务器对应用户.ssh目录下有authorized_keys文件,也就是将服务器上生成的公钥文件保存为authorized_keys。并将私钥文件作为paramiko的登陆密钥
transport封装密钥登陆:
#!/usr/bin/env python
#coding:utf8
#必须先将公钥文件传输到服务器的~/.ssh/authorized_keys中
import paramiko
# 指定本地的RSA私钥文件,如果建立密钥对时设置的有密码,password为设定的密码,如无不用指定password参数
pkey = paramiko.RSAKey.from_private_key_file('id_rsa_1024') #创建transport对象绑定主机和端口,指定用户和密钥连接
transport = paramiko.Transport(('172.16.32.129',2323))
transport.connect(username='root',pkey=pkey)
ssh = paramiko.SSHClient()
ssh._transport = transport #类属性赋值
#允许将信任的主机自动加入到known_hosts列表
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #执行命令
stdin,stdout,stderr=ssh.exec_command('free -m')
print(stdout.read().decode())
ssh.close()
(3)SFTP文件传输
#!/usr/bin/env python
#coding:utf8 import paramiko
#实例化transport对象,并建立连接
transport = paramiko.Transport(('172.16.32.129',2323))
transport.connect(username='root',password='123.com')
#实例化sftp对象,指定连接对象
sftp = paramiko.SFTPClient.from_transport(transport)
#上传文件
sftp.put(localpath='id_rsa_1024',remotepath='/root/idrsa1024.txt')
#下载文件
sftp.get(remotepath='/root/idrsa1024.txt',localpath='idrsa1024_back.txt')
#关闭连接
transport.close()
实现一个类似xshell工具的功能,登录以后可以输入命令回车后就返回结果:
import paramiko
import os
import select
import sys # 建立一个socket
trans = paramiko.Transport(('192.168.2.129', 22))
# 启动一个客户端
trans.start_client() # 如果使用rsa密钥登录的话
'''
default_key_file = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa')
prikey = paramiko.RSAKey.from_private_key_file(default_key_file)
trans.auth_publickey(username='super', key=prikey)
'''
# 如果使用用户名和密码登录
trans.auth_password(username='super', password='super')
# 打开一个通道
channel = trans.open_session()
# 获取终端
channel.get_pty()
# 激活终端,这样就可以登录到终端了,就和我们用类似于xshell登录系统一样
channel.invoke_shell()
# 下面就可以执行你所有的操作,用select实现
# 对输入终端sys.stdin和 通道进行监控,
# 当用户在终端输入命令后,将命令交给channel通道,这个时候sys.stdin就发生变化,select就可以感知
# channel的发送命令、获取结果过程其实就是一个socket的发送和接受信息的过程
while True:
readlist, writelist, errlist = select.select([channel, sys.stdin,], [], [])
# 如果是用户输入命令了,sys.stdin发生变化
if sys.stdin in readlist:
# 获取输入的内容
input_cmd = sys.stdin.read(1)
# 将命令发送给服务器
channel.sendall(input_cmd) # 服务器返回了结果,channel通道接受到结果,发生变化 select感知到
if channel in readlist:
# 获取结果
result = channel.recv(1024)
# 断开连接后退出
if len(result) == 0:
print("\r\n**** EOF **** \r\n")
break
# 输出到屏幕
sys.stdout.write(result.decode())
sys.stdout.flush() # 关闭通道
channel.close()
# 关闭链接
trans.close()
支持tab自动补全
import paramiko
import os
import select
import sys
import tty
import termios '''
实现一个xshell登录系统的效果,登录到系统就不断输入命令同时返回结果
支持自动补全,直接调用服务器终端 '''
# 建立一个socket
trans = paramiko.Transport(('192.168.2.129', 22))
# 启动一个客户端
trans.start_client() # 如果使用rsa密钥登录的话
'''
default_key_file = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa')
prikey = paramiko.RSAKey.from_private_key_file(default_key_file)
trans.auth_publickey(username='super', key=prikey)
'''
# 如果使用用户名和密码登录
trans.auth_password(username='super', password='super')
# 打开一个通道
channel = trans.open_session()
# 获取终端
channel.get_pty()
# 激活终端,这样就可以登录到终端了,就和我们用类似于xshell登录系统一样
channel.invoke_shell() # 获取原操作终端属性
oldtty = termios.tcgetattr(sys.stdin)
try:
# 将现在的操作终端属性设置为服务器上的原生终端属性,可以支持tab了
tty.setraw(sys.stdin)
channel.settimeout(0) while True:
readlist, writelist, errlist = select.select([channel, sys.stdin,], [], [])
# 如果是用户输入命令了,sys.stdin发生变化
if sys.stdin in readlist:
# 获取输入的内容,输入一个字符发送1个字符
input_cmd = sys.stdin.read(1)
# 将命令发送给服务器
channel.sendall(input_cmd) # 服务器返回了结果,channel通道接受到结果,发生变化 select感知到
if channel in readlist:
# 获取结果
result = channel.recv(1024)
# 断开连接后退出
if len(result) == 0:
print("\r\n**** EOF **** \r\n")
break
# 输出到屏幕
sys.stdout.write(result.decode())
sys.stdout.flush()
finally:
# 执行完后将现在的终端属性恢复为原操作终端属性
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty) # 关闭通道
channel.close()
# 关闭链接
trans.close()
python3之paramiko模块的更多相关文章
- [转]python3之paramiko模块(基于ssh连接进行远程登录服务器执行命令和上传下载文件的功能)
转自:https://www.cnblogs.com/zhangxinqi/p/8372774.html 阅读目录 1.paramiko模块介绍 2.paramiko的使用方法 回到顶部 1.para ...
- Python3学习之路~9.1 paramiko模块:实现ssh执行命令以及传输文件
我们一般使用linux的时候,都是在Windows上安装一个ssh客户端连接上去.那么从一台linux如何连接到另一条linux呢?使用ssh命令即可,因为每台linux机器自己都有一个ssh客户端. ...
- debian+python3.5环境下安装paramiko模块:
debian+python3.5环境下安装paramiko模块: 1.确保安装了操作系统安装了libssl-dev,zlib1g-dev (redhat,centos下这两包包名为openssl-d ...
- Python3下的paramiko模块
paramiko模块是基于Python实现的SSH远程安全连接,用于SSH远程执行命令.文件传输等功能. 默认Python没有,需要手动安装:pip install paramiko SSH密码认证远 ...
- python实战第一天-paramiko模块并练习
操作系统 Ubuntu 15.10 IDE & editor JetBrains PyCharm 5.0.2 ipython3 Python版本 python-3.4.3 安装paramiko ...
- Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块
Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fab ...
- python第四十九天--paramiko模块安装大作战
准备开始学习:paramiko模块,发现这个模块十分难搞 安装不上 搞了半天,win10 64下 pytyon 3.6 的 paramiko模块 死活安不上,在网上不断的找资料,可是没有用,没有用啊 ...
- Python远程连接主机之paramiko模块
Python的paramiko模块能够连接远程主机,并在该主机上执行命令,和该主机之间进行文件传输.paramiko支持用明文密码登录远程主机和秘钥登录.使用之前要安装一下这个模块哈,pip inst ...
- 审计系统---paramiko模块学习
paramiko模块学习 [更多参考]http://www.cnblogs.com/wupeiqi/articles/4963027.html [paramiko的Demo实例]https://git ...
随机推荐
- 提升HTML5的性能体验系列之三 流畅下拉刷新和上拉
下拉刷新 为实现下拉刷新功能,大多H5框架都是通过DIV模拟下拉回弹动画,在低端android手机(Android4.4以下)上,DIV动画经常出现卡顿现象(特别是图文列表的情况).解决方案还是web ...
- python入门之深浅copy
a1=["a","b","c","aa"] b1=a1 a1[0]=" print(a1,b1) 此时结果为: ...
- usart下位机输出使用printf的格式化技巧
输出使用printf("0x%3X ", I2c_Buf_Write[i]);时,上位机接收过程,使用文本格式,显示结果如下 虽然是保留了三位的宽度,但是并不美观. 相比于使用pr ...
- kbmmw 5.06.20 发布
经过1个多月的测试,终于发布正式版. 5.06.20 July 11 2018 Important notes (changes that may break existing c ...
- Ajax复习
1.标准请求响应时浏览器的动作(同步操作) 1.1 浏览器请求什么资源,跟随显示什么资源 2.ajax:异步请求: 2.1 局部刷新,通过异步请求,请求到服务器资源数据后,通过脚本修改页面中部分内容 ...
- 63.delegate回调 和block回调
效果:viewController里面放置一个按钮和Lab,点击按钮进入oneViewController(delegate回调)或者BlockViewController(block回调),两者控制 ...
- iOS知识基础篇 static
static关键字的作用 一.隐藏 通过static修饰的函数或者变量,在该文件中,所有位于这条语句之后的函数都可以访问,而其他文件中的方法和函数则不行: 二.静态变量 类方法不可以访问实例变量(函 ...
- Graphviz 环境变量设置
今天晚上解决了一个错误,如下:
- tomcat 服务器故障排除
故障现象 通过浏览器访问tomcat服务器发现服务器没有响应. 问题分析检查 登陆服务器发现,TOMCAT服务器并没有宕机,服务还在. 使用JPS命令查看了一下tomcat的进程ID,获取进程ID后, ...
- C语言常用数据类型说明
1.取值范围: short一般占两个字节,取值范围:-32768 - 32767 int一般占两个或四个字节,取值范围:-2147483648 - 2147483647 unsigned int ...