python forward remote tcp port to local port

Viewed 32



I have the following setup: setup diagram

The unit constantly tries to connect to the Remote Server on a specific known port.
On the Remote Server, there is nothing but open TCP ports.
I want to forward the Remote Server's port to My Pc and open a TCP Server to read the data.

Eventually, I want to use Python to implement this,
but in the meantime, I'm trying to use ssh to do this: ssh -N -R 10000:localhost:10000 username@hostname,
and on my side (My Pc), I tried to open a socket (with python) to listen to port 10000, and tried to open Hercules to simulate a TCP server, however, I didn't receive any data.

obviously, something is missing, what is it?
p.s. opening a TCP server on the Remote Server will get the data,
but I need to control the connections from My Pc.

If needed I will provide the python code that is using sshtunnel.SSHTunnelForwarder (which is not working )

this should be the Python code to implement the ssh tunnel:

from sshtunnel import SSHTunnelForwarder
import socket

SSH_SERVER = 'hostname'
SSH_USERNAME = 'ubuntu'
SSH_PASSWORD = 'password'
PORT = 10_000

with SSHTunnelForwarder(ssh_address_or_host=SSH_SERVER,
                        ssh_username=SSH_USERNAME,
                        ssh_password=SSH_PASSWORD,
                        remote_bind_address=('0.0.0.0', PORT),
                        local_bind_address=('127.0.0.1', PORT),
                        ) as server:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(('127.0.0.1', PORT))
        s.listen(1)
        conn, addr = s.accept()
        with conn:
            print(f"Connected by {addr}")
            data = conn.recv(128)
            print(data)

for MVP:
here is a unit simulator:

import socket

SSH_SERVER = 'hostname'
PORT = 10_000

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((SSH_SERVER, PORT))
    s.sendall(b"Hello, world")

I want to see the b"Hello, world" in the print(data)

0 Answers
Related