# 使用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服務
red = redis.Redis(host='127.0.0.1', port=server.local_bind_port, db=db, decode_responses=True) #如果設置了密碼,就加上password=密碼
red.set('name','lily')
red.get('lily')
server.close() # 關閉隧道
參考:https://blog.csdn.net/windy135/article/details/79404924