How can I create a ssh tunnel using python like 'ssh -D'? - python

I have try to implement it using paramiko. But I find paramiko can only launch a tunnel redirecting localhost:lport to remoteip:rport just like 'ssh -L'.
So I want to know how can I implement it using paramiko or use other library.
Thanks for your help.

Unlike the -L or -R options which forward specific ports, ssh -D establishes a SOCKS proxy for application-level port forwarding.
Assuming that's what you want, then according to this answer, there are several Python modules which implements SOCKS clients/proxies, e.g. PySocks and SocksiPy. Unfortunately, I have never explored any of these options and cannot vouch for any of them.

Related

Port forwarding in python: why not just use ssh -L?

At [TO_PORT] on the server [TO_ADDRESS] is something I would like to connect with (Theia IDE). Using Linux I can simply do: ssh -L [FROM_PORT]:localhost:[TO_PORT] [TO_ADDRESS]. Now I can access the Theia IDE using localhost:[FROM_PORT].
When researching how to do port forwarding in a python program I found many solutions and all looked quite bulky. Why do people implement their own port forwarding and not use the ssh command?

Paramiko equivalent for OpenSSH directives PreferredAuthentications=password and PubkeyAuthentication=no

Can I get Paramiko code for the following ssh command?
ssh Administrator#xx.xx.xx.xx -vv -o PreferredAuthentications=password -o PubkeyAuthentication=no
Need to know how to handle fields PreferredAuthentications and PubkeyAuthentication in Paramiko.
There's no direct equivalent, as Paramiko has different logic than ssh when selecting the authentication methods. And you actually didn't tell us why are you using those directives.
If your point was to avoid using autodiscovered key files, use allow_agent=False and look_for_keys=False.
See Force password authentication (ignore keys in .ssh folder) in Paramiko in Python

Emulating SSH's SOCKS Proxy Tunnel in Python

I used to create a SOCKS connection between a windows client and linux server using SSH server and putty. However, the firewall between the client and server is now able to identify SSH packets and drop them.
I was wondering if I can emulate such behavior of SSH tunnels using python? Any recommendations on libraries or readings?
Thanks in advance.
Yes, yes you can. Pick your poison.
http://socksipy.sourceforge.net/
http://sourceforge.net/projects/pysocks/
http://code.google.com/p/socksipy-branch/
How can I use a SOCKS 4/5 proxy with urllib2?
http://google-api-python-client.googlecode.com/hg/docs/httplib2.socks.html
You can consider using paramiko for your SSH. Here is a nice link ssh-programming-with-paramiko
You can also try this ssh module which uses paramiko.

open an ssh tunnel from heroku python app on the cedar stack?

Is it possible to open a non-blocking ssh tunnel from a python app on the heroku cedar stack? I've tried to do this via paramiko and also asyncproc with no success.
On my development box, the tunnel looks like this:
ssh -L local_port:remote_server:remote_port another_remote_server
Can you please post the STDERR of ssh -v -L .....? May be you need to disable the tty allocation and run ssh in batch mode.
This recipe ought to work with Python (even though it was for a Rails app). Here's the recipe: https://stackoverflow.com/a/27361295/558639
The biggest challenge is convincing ssh to not prompt when it starts up.

How to run a fabric script over a SOCKS proxy?

I have a SOCKS proxy setup to a gateway server which is created by setting up a host definition in my ssh_config to use DynamicForward localhost:9876. To connect with SSH to the remote server I've setup another host definition to use ProxyCommand /usr/bin/nc -x localhost:9876 %h %p which is all working fine.
However my fabric script needs to connect to that remote server. How can I tell it to use the SOCKS proxy when it connects?
Tunnelling is now (March 2013) supported by Fabric natively: see the discussion leading to the changes in code and the commit message with a bit of rationale.
Fabric's SSH layer does not support gateways or proxies at the moment but they "may end up patching/forking it sometime to add that functionality." (from here).
There's also an outstanding issue on Fabric to implement tunneling.
This blog post suggests overriding the run function.
You can use tsocks and the OpenSSH client's built-in support for creating SOCKS tunnels. It requires a bit of configuration but it works fine. Below is how I got started on Ubuntu 10.04.
# Install the tsocks library and shell script.
sudo apt-get install tsocks
# Configure the range of IP addresses you need access to.
sudo nano /etc/tsocks.conf
# Use the OpenSSH client to create a socks proxy (stepping
# stones are hosts used to gain access to private subnets).
ssh -D 1080 stepping.stone
# Now connect to any given address in your configured range.
# tsocks will intercept the connection and route it using
# the SOCKS proxy we created with the previous command.
tsocks ssh 1.2.3.4
The fact that Fabric doesn't work without a VPN connection was a deal breaker for me, so this is a great solution; all it requires is SSH access to one host.
on Fabric (1.12.0)(doc):
env.use_ssh_config = True

Categories

Resources