python reptile tutorial: Connecting redis way through the tunnel SSHTunnelForwarder

Today small for everyone to share the connection method of a python redis by SSHTunnelForwarder tunnel, a good reference value, we want to help. Come and see, to follow the small series together
Background: Our Redis server to use Amazon services, local needs by stepping stones, and then have access to Redis service.

Connection principle: Use SSHTunnelForwarder module, ssh into stepping stones through the local port 22, and then open a local port forwarding to a remote machine springboard Redis service use.

Two ideas:

1, by SSHTunnelForwarder, paramiko module, the first ramp ssh machine and dryer on the ramp (or the internal server), access permissions, then the remote Redis.

2, using SSHTunnelForwarder module, the local machine through the SSH springboard to port 22, and then open a local port to a forwarding service uses Redis springboard remotely.

A thought:

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

similar:

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!')

Method Two:

# 使用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:

General springboard machine is a clean machine, most of the company network or the server does not have permission to redis-client clients
recommend our learning Python buckle qun: 913066266, look at how seniors are learning! From basic web development python script to, reptiles, django, data mining, etc. [PDF, actual source code], zero-based projects to combat data are finishing. Given to every little python partner! Every day, Daniel explain the timing Python technology, to share some of the ways to learn and need to pay attention to small details, click to join our gathering python learner

This connection method described above python redis by SSHTunnelForwarder tunnel is small series to share the entire contents of all of the

Released seven original articles · won praise 1 · views 3557

Guess you like

Origin blog.csdn.net/haoxun12/article/details/104954808