![]() '-L', '".format(tunnel.local_port, tunnel. Subprocess.call(['curl', ' Here's a little class that you can drop into your code: import subprocessÄef _init_(self, host, user, port, key, remote_port): Tunnel = SshTunnel(2222, 80, 'karel', 'localhost') Raise Exception ('ssh tunnel setup failed') ![]() '-L', str(self.localport) + ':' + self.remotehost + ':' + str(self.remoteport), Self.daemon = True # So that thread will exit when Self.remotehost = remotehost # What host do we send traffic to Self.remoteuser = remoteuser # Remote user on remotehost Self.remoteport = remoteport # Remote port on remotehost Self.localport = localport # Local port to listen to ssh -i .pem -o Proxåommand'ssh -i .This actually reverses the tunneling slightly, as the bastion host now relays the SSH connection and not the web connections. import subprocessÄef _init_(self, localport, remoteport, remoteuser, remotehost): First find a way to compress it it down to one SSH command. Once SshTunnel sets up a local TCP port, you can connect to it - be it via your MySQL client, curl, or whatever. I didn't put in a full MySQL connectivity example because it should be self-explanatory. The ssh tunnel thread is marked as daemon so that it will automatically stop once the main activity terminates. The thus running ssh tunnel is on one thread the main task must be in another one. All it does, is start SSH in the following way: ssh -N -L localport:remotehost:remoteport order to make this work, you'll need a password-less login for (via ~/.ssh/id_rsa.pub that's known on the remote server). The class SshTunnel is initialized with 4 parameters, the local and remote port, the remote user, and the remote host. ![]() ![]() In the example above, the remote server is running a MySQL database on port 3306. The main activity consists of running curl ie., fetching a webpage but from port 2222. A SSH connection will be established from My computer to SSH server (used as a jump host) and the local clients will use this tunnel in order to directly connect to the remote server. In this example, the ssh tunnel forwards local port 2222 to port 80 on localhost. It runs an SSH tunnel in a separate thread then the main thread does something to get network traffic over the SSH tunnel. Here is a code snippet for Python3 (but you should be able to retrofit it into Python2 without difficulty). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |