python 通过SSHTunnelForwarder隧道连接redis的方法
作者:__Jack 时间:2021-03-08 12:58:41
背景:我司Redis服务器使用的亚马逊服务,本地需要通过跳板机,然后才有权限访问Redis服务。
连接原理:使用SSHTunnelForwarder模块,通过本地22端口ssh到跳板机,然后本地开启一个转发端口给跳板机远程Redis服务使用。
两种思路:
1、通过SSHTunnelForwarder,paramiko模块,先ssh到跳板机,然后在跳板机上(或者内部服务器上),获取到权限,然后远程Redis。
2、使用SSHTunnelForwarder模块,通过本地22端口ssh到跳板机,然后本地开启一个转发端口给跳板机远程Redis服务使用。
思路一:
private_key_path = '/Users/xxx/.ssh/id_rsa'
rsaKey = paramiko.RSAKey.from_private_key_file(private_key_path)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(跳板机或者内网服务器IP或者域名, 22, username, rsaKey)
stdin, stdout, stderr = ssh.exec_command('redis-cli -h {} -p {} -n {} {}'.format(host, port, db, script))
result = stdout.read(), stderr.read()
for out in result: # 需要通过循环拿到stdout,否则为空值
if out:
return out
类似:
import paramiko
from sshtunnel import SSHTunnelForwarder
with SSHTunnelForwarder(
(REMOTE_SERVER_IP, 443),
ssh_username="",
ssh_pkey="/var/ssh/rsa_key",
ssh_private_key_password="secret",
remote_bind_address=(PRIVATE_SERVER_IP, 22),
local_bind_address=('0.0.0.0', 10022)
) as tunnel:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('127.0.0.1', 10022)
# do some operations with client session
client.close()
print('FINISH!')
方法二:
# 使用SSHTunnelForwarder隧道,通过跳板机链接Redis
with SSHTunnelForwarder(
('xxx.xxx.xx.xx', 22), # 跳板机
ssh_username=username,
ssh_pkey="/Users/xxx/.ssh/id_rsa",
remote_bind_address=('xx.xx.xx.xxx', 6379), # 远程的Redis服务器
local_bind_address=('0.0.0.0', 10022) # 开启本地转发端口
) as server:
server.start() # 开启隧道
print(server.local_bind_port)
# 本地通过local_bind_port端口转发,利用跳板机,链接Redis服务
cls.red = redis.Redis(host='127.0.0.1', port=server.local_bind_port, db=db, decode_responses=True)
server.close() # 关闭隧道
Advice:
一般跳板机是个干净的机器,公司内网服务器大部分不会给权限或者有redis-client客户端,因此推荐使用方法2。
SSHTunnelForwarder使用:https://pypi.python.org/pypi/sshtunnel/
来源:https://blog.csdn.net/windy135/article/details/79404924
标签:python,SSHTunnelForwarder,redis
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ASP 日期的加减运算实现代码
2011-03-08 10:47:00
Flask框架踩坑之ajax跨域请求实现
2023-12-25 01:28:02
![](https://img.aspxhome.com/file/2023/3/80703_0s.png)
python实现按关键字筛选日志文件
2022-12-28 21:45:49
python批量下载网站马拉松照片的完整步骤
2023-08-31 19:00:27
![](https://img.aspxhome.com/file/2023/5/64985_0s.jpg)
关于opencv读取和写入路径有汉字的处理方式
2021-10-25 18:00:09
![](https://img.aspxhome.com/file/2023/6/80496_0s.jpg)
ORACLE常见错误代码的分析与解决(一)
2010-08-02 13:20:00
Python基础学习之反射机制详解
2023-02-15 11:02:01
asp 随机字符串函数
2011-04-04 11:01:00
实例讲解在MySQL中如何导出整个数据库
2009-09-01 10:03:00
让你一文弄懂Pandas文本数据处理
2023-07-17 19:12:08
![](https://img.aspxhome.com/file/2023/8/61218_0s.png)
解决pycharm启动后总是不停的updating indices...indexing的问题
2023-03-05 07:35:38
Python request post上传文件常见要点
2022-11-05 09:27:14
![](https://img.aspxhome.com/file/2023/5/80485_0s.png)
ASP教程:0177:800401f3错误解决
2008-08-02 12:41:00
Javascript学习第一季 三
2008-06-27 13:08:00
oracle ORA-01114、ORA-27067错误解决方法
2023-07-15 18:36:39
Python seek()和tell()函数的具体使用
2023-12-07 20:59:39
![](https://img.aspxhome.com/file/2023/2/67242_0s.gif)
Django实现简单登录的示例代码
2022-03-26 04:10:18
![](https://img.aspxhome.com/file/2023/1/86631_0s.png)
MSSQL数据类型
2008-08-03 17:21:00
操作mysql数据库的类
2010-03-13 12:53:00
python通过imaplib模块读取gmail里邮件的方法
2023-11-03 04:24:42