首页 > 其他分享 >paramiko模块使用

paramiko模块使用

时间:2022-09-02 18:00:56浏览次数:45  
标签:模块 paramiko stdout private ssh key 使用 transport

该模块基于ssh用于连接远程服务器并执行相关操作。
SSHClient 用于连接远程服务器并执行基本命令

pip3 install paramiko

 

# 基于用户名和密码
import paramiko

# 创建ssh对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='10.0.0.12', port=22, username='root', password='mysql')
# 执行命令
stdin, stdout, stderr = ssh.exec_command('df -Th')
# 获取命令结果
result = stdout.read().decode('utf8')
# 关闭连接
ssh.close()
stdout.close()
print(result)

 

# SSHClient封装Transport
import paramiko
transport = paramiko.Transport(('10.0.0.12', 22))
transport.connect(username='root', password='mysql')
ssh = paramiko.SSHClient()
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command('df')
result = stdout.read().decode()
transport.close()
print(result)

 

# 基于公钥和私钥,请提前执行ssh-copy-id
# ssh-copy-id -i 公钥 [email protected]
# ssh-copy-id -i id_rsa.pub [email protected]
# ssh [email protected]
import paramiko

# 私钥的地址
private_key = paramiko.RSAKey.from_private_key_file('/Users/zhouwanchun/.ssh/id_rsa')
# 创建ssh对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='10.0.0.12', port=22, username='root', pkey=private_key)
# 执行命令
stdin, stdout, stderr = ssh.exec_command('df -Th')
# 获取命令结果
result = stdout.read().decode('utf8')
# 关闭连接
ssh.close()
stdout.close()
print(result)

 

# SSHClient封装Transport
import paramiko
private_key = paramiko.RSAKey.from_private_key_file('/Users/zhouwanchun/.ssh/id_rsa')
transport = paramiko.Transport(('10.0.0.12', 22))
transport.connect(username='root', pkey=private_key)
ssh = paramiko.SSHClient()
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command('df -Th')
result = stdout.read().decode()
transport.close()
print(result)

 

# 基于私钥字符串进行连接
import paramiko
from io import StringIO

key_str = """-----BEGIN OPENSSH PRIVATE KEY-----
b2BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAYEA3lzSsOqVI/3JJqLuYGUMGb31vkP8h1K2X73A956LLHkfZt1hrbz/
GyzFTcwSw5Sioh4MD7vXkaYWzgZJpBpbEdBpoQZuaowJuygE3rMwwgdsETmmTRY7EogiVj
BrUKPWg/OmF6/ZmkIzNCDlSJlGmhCYq8T0NMEp60VO3r129aszleDfXd2UzOLUKPjOl8rk
Jk/5pN6BcjZ8f5kKfKtQxGKjq5HQ0wPVrmYBRNo5qRvlAx2LRBJpnw3ZLJ1TsH1zHyNO4Y
8fmUsdGbvNYpKPgg+fip4AL0QdKv7k8kRFXicg0l4zbrnRzI0L/yb4p36vb896efamHOWA
SXWyDiG2UsJJR0szd9gtMrQwvVODvJsb52RAPvrumDjkInWyiZtCeFA/ctZWbbuhmUV4zw
IO1STZZgA2iiq23XDLvPa9ceUOEHFfGG2dmWE2/M41iGGoiKwvtgj/lqrtNDjkZ/+YEr9o
7r24OEJqebCTkSzmfd4DtLVPOdNIVBCpktmwJpsLAAAFmA7XThMO104TAAAAB3NzaC1yc2
EAAAGBAN5c0rDqlSP3ySai7mBlDBm99b5D/IdStl+9wPeeiyx5H2bdYa28/xssxU3MEsOU
oqIeDA+715GmFs4GSaQaWxHQaaEGbmqMCbsoBN6zMMIHbBE5pk0WOxKIIlYwa1Cj1oPzph
ev2ZpCMzQg5UiZRpoQmKvE9DTBKetFTt69ZfWrM5Xg312dlMzi1Cj4zpfK5CZP9qTegXI2
fH+ZCnyrUMRio6uR0NMD1a5mAUTaOakb5QMdi0QSaZ8N2SydU7B9cx8jTuGPH5lLHRm7zW
KSj4IPn4qeAC9EHSr+5PJERV4nINJeM2650cyNC/8m+Kd+r2/Penn2phzlgEl1sg4htlLC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DhtRlSgsyzc9pqCEcK6B2X/slrtTvqdaa8lWbU8rMvvWEXxaCTjbanrOhw
66/2c1AFS2IBYP0qDSGn4YqiaDjXeLrk7z2RC9RipPtrRgjTyGaj8housUUu5Kf2zhp8b5
D1NrrSm6WLMAgcLm+LDqr3+uBwZrH+VQAAAMEA6sTafjrZ28nlgcWz/twOl2R1xmQncIKa
AYNoVdlfdDoK9wsOYhZvWaoth1UC2aK8zNzp1VfPAAwHfeqVv2zRR3WEUFU4gXo7D9NnNx
CgJC0IYadxjF+KUW+3OZeinbbJkuFTS8DemyHp9JvVNBepCnFRvfD+GH3d06f+FxU00cfA
cJOtk7+5+MEmKA8gXk+U0zezLkgu53PUEFs7+UeNWuA80vmkHFn+b+wmcsKNRwWTYE/4Kc
D5BK0x/MySktPfAAAAH3pob3V3YW5jaHVuQGxvdmVteXNxbDMzMDYubG9jYWwBAgM=
-----END OPENSSH PRIVATE KEY-----"""

private_key = paramiko.RSAKey(file_obj=StringIO(key_str))
transport = paramiko.Transport(('10.0.0.12', 22))
transport.connect(username='root', pkey=private_key)
ssh = paramiko.SSHClient()
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command('df')
result = stdout.read().decode()
transport.close()
print(result)

 

# SFTPClient 用于连接远程服务器并执行上传下载文件
# 基于用户名和密码
import paramiko
transport = paramiko.Transport(('10.0.0.12', 22))
transport.connect(username='root', password='mysql')
sftp = paramiko.SFTPClient.from_transport(transport)
# 将local_path上传到remove_path
sftp.put('/tmp/1.py', '/tmp/test.py')
# 将remove_path下载到local_path
sftp.get('/tmp/test.py', '/tmp/2.py')
transport.close()


# 基于公钥和私钥
import paramiko
private_key = paramiko.RSAKey.from_private_key_file('/Users/zhouwanchun/.ssh/id_rsa')
transport = paramiko.Transport(('10.0.0.12', 22))
transport.connect(username='root', pkey=private_key)
sftp = paramiko.SFTPClient.from_transport(transport)
# 将local_path上传到remove_path
sftp.put('/tmp/1.py', '/tmp/test.py')
# 将remove_path下载到local_path
sftp.get('/tmp/test.py', '/tmp/2.py')
transport.close()

 

标签:模块,paramiko,stdout,private,ssh,key,使用,transport
From: https://www.cnblogs.com/zhouwanchun/p/16650811.html

相关文章

  • gzip 介绍和使用
    gzip介绍和使用用法gzip[OPTION]...[FILE]...常用参数-d--decompress解压-c--stdout保留原始文件,把压缩/解压流重定向到新文件(如:gzip-caa>aa.gz)-l......
  • optparse模块使用
    #!/usr/bin/envpython3#-*-coding:utf-8-*-#python中有两个内建模块用来处理命令行参数:getpot、optparse。#optparse模块主要用来为脚本传递命令参数,采用预定......
  • Docker 个人使用的一些镜像及记录
    Docker修改源创建或修改/etc/docker/daemon.json文件{"registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]}Docker中国区官方镜像https://regist......
  • pyuic5和pyrcc的使用方法
    一、如果是使用QtDesigner设计界面的话,那么如何将QtDesigner设计出来的界面(.ui文件)与业务逻辑程序接合起来,如下两个方法:方法一:将.ui文件通过命令转换成.py文件,然后在......
  • Apache Maven ToolChains的使用
    ApacheMavenToolChains的使用  简介Toolchains的介绍Toolchains的例子Toolchains支持总结简介Maven是java中非常有用和常用的构建工具,基本上现在大型的java项目都是M......
  • Linux命令-按照与使用(13)为初学者介绍的 Linux tee 命令(6 个例子)
    转载地址:为初学者介绍的Linuxtee命令(6个例子)有时候,你会想手动跟踪命令的输出内容,同时又想将输出的内容写入文件,确保之后可以用来参考。如果你想寻找这相关的工具,那......
  • python的常用方法和模块
    1.str类下的方法Pythonzfill()方法返回指定长度的字符串,原字符串右对齐,前面填充0......
  • Flask 学习-40.Flask-RESTful 结合蓝图使用
    前言Flask-RESTful结合蓝图使用设计接口RESTful接口没使用蓝图之前注册接口fromappsimportcreate_app,db,jwtfromflaskimporturl_for,request,jsonifyf......
  • Spring Boot 2.x基础教程:使用 ECharts 绘制各种华丽的数据图表
    通常,这类需求在客户端应用中不太会用到,但是在后端的各种统计分析模块会经常碰到。比如:通过折线图、柱状图、雷达图等可视化形式,更直观的展现和分析经营状况或系统运行情况......
  • time包使用
    ✍️time包在实际中经常用到,可以对比python中的time库进行学习1、获取年月日functimeDemo(){ now:=time.Now()//获取当前时间 fmt.Printf("currenttime:%v\n",......