[转]python3之paramiko模块(基于ssh连接进行远程登录服务器执行命令和上传下载文件的功能)
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模块(基于ssh连接进行远程登录服务器执行命令和上传下载文件的功能)的更多相关文章
- paramiko模块(基于SSH用于连接远程服务器)
paramiko模块,基于SSH用于连接远程服务器并执行相关操作 class SSHConnection(object): def __init__(self, host_dict): self.ho ...
- mac 下配置连接Linux服务器方法,上传下载文件操作
1.先按照文档在本地生成SSHkey 2.mac输入 sudo -i 进入超级管理员#模式下,然后 创建用户 #useradd XXXadmin #passwd XXXadmin XXXadmin用户 ...
- 使用paramiko远程登录并执行命令脚本
#!/usr/bin/env python #coding=utf-8 import paramiko, getpass,sys,traceback class ssh_utils(): def lo ...
- 用C#连接SFTP服务器并进行上传下载文件
1.使用软件连接可采用WinSCP进行: 文件协议选择SFTP,端口号默认22 2.使用C#代码操作 参考:http://www.cnblogs.com/binw/p/4065642.html 主要引 ...
- python3之paramiko模块
1.paramiko模块介绍 paramiko模块提供了基于ssh连接,进行远程登录服务器执行命令和上传下载文件的功能.这是一个第三方的软件包,使用之前需要安装. 2.paramiko的使用方法 (1 ...
- Python3-paramiko模块-基于SSH的远程连接模块
Python3中的paramiko模块,基于SSH用于连接远程服务器并执行相关操作 http://docs.paramiko.org/en/2.1/ SSHClient 用于连接远程服务器并执行基本命 ...
- python利用paramiko连接远程服务器执行命令
python中的paramiko模块是用来实现ssh连接到远程服务器上的库,在进行连接的时候,可以用来执行命令,也可以用来上传文件. 1.得到一个连接的对象 在进行连接的时候,可以使用如下的代码: d ...
- Python之paramiko模块和SQL连接API
堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: i ...
- CMDB-(paramiko模块 -- 实现ssh连接)
import paramiko # 实现ssh功能的模块 ssh = paramiko.SSHClient() # 实例化对象 ssh.set_missing_host_key_policy(para ...
随机推荐
- [HNOI2003]消防局的设立 树形dp // 贪心
https://www.luogu.org/problemnew/show/P2279 一开始就想到了贪心的方法,不过一直觉得不能证明. 贪心的考虑是在深度从深到浅遍历每个结点的过程中,对于每个没有覆 ...
- 怎么停止yum安装并kill进程
1. ctrl + z 2. ps -ef | grep 正在安装的包名称 3. kill -9 进程Id
- TCP详解——连接建立与断开
一.报文结构介绍 在开始讲TCP连接过程时,还是先看看TCP报文的格式如图1所示.IP数据报此时由IP头部+TCP头部+TCP数据组成.不带选项的TCP头部是20字节长,而带选项的,TCP头部最长可达 ...
- OS + RedHat 6.3 x64 NFS / mount.nfs: access denied by server while mounting
s Linux mount/unmount命令(转) https://www.cnblogs.com/xd502djj/p/3809375.html 问题2:NFS配置项no_root_squash和 ...
- Unity 如何检测鼠标双击事件
代码如下: void OnGUI(){ Event e=Event.current; )) Debug.Log("用户双击了鼠标"); }
- CentOS7 下编译 Hadoop
准备工作 下载 Hadoop 源码 Source (当前最新 2.9.2) https://hadoop.apache.org/releases.html 打开压缩包会看到 BUILDING.txt ...
- PHP索引数组+unset使用不当导致的问题
转自先知社区 https://xz.aliyun.com/t/2443 0x00前言 通常网站后台可以配置允许上传附件的文件类型,一般登录后台,添加php类型即可上传php文件getshell.但是, ...
- Shell编程(三)Shell特性
!$:显示上一条命令最后一个参数 $?: 上个命令的退出状态,或函数的返回值. alias xxx="命令":给命令取别名 xxx 通过 vim ~/.bashrc 里编辑,可以来 ...
- Set实现数组去重
ES6 提供了新的数据结构 Set 它类似于数组,但是成员的值都是唯一的,没有重复的值 (set本身是一个构造函数,用来生成 Set 数据结构) 使用Set实现数组去重要简单很多. 第一种数组 ...
- javascript&&jquery编写插件模板
javascrpt插件编写模板 这里不分享如何编写插件,只留一个框架模板,使用面向对象的形式进行编写,方便管理 ;(function(window,document){ function FnName ...
