run ipython notebook remotely through ssh tunneling - python

I am wondering if I can use ipython notebook remotely by ssh twice.
The scenario is: Machine B is the machine I want to run ipython notebook. However, I am only allowed to access machine B through another server (machine A) first.
There are tutorials about using ipython notebook remotely, but none of them mentions the situation I've encountered.
Thanks in advance !

Assuming you are referring to ssh tunnelling, and the ipython notebook is servering on port 1234 on machine B:
If machine A can access machine B on any port, you can setup machine A to forward a remote port to you via SSH:
ssh -L 9999:machineB.com:1234 -N machineA.com
This says
ssh to machineA.com without executing a remote command (-N) and setup machine A to forward requests from client port 9999, over an ssh tunnel, to machine B port 1234
However if machine A can only access machine B via ssh, then you will need to create two tunnels. One from your client PC to machineA, and another from machineA to machineB. To do this, the two tunnels connect to a local port on machineA instead of a remote port:
ssh -L 9999:localhost:8888 machineA.com ssh -L 8888:localhost:1234 -N machineB.com
This says
ssh to machineA.com and setup machine A to forward requests from our client PC port 9999, over an ssh tunnel, to machine A port 8888. Then execute the command "ssh -L 8888:localhost:1234 -N machineB.com". This command sets up a second tunnel from machineA port 8888 to machineB port 1234 (where iPython is listening).
Now, with that command running in the background, connect to your local PC port 9999. The first ssh tunnel will forward that request to machineA where it connects to localhost:8888, the second ssh tunnel will then forward it to machineB where it connects to localhost:1234.
Note that machineA will need to be able to connect to machineB automatically (using public/private key authentication) for this to work in a single command.
Here is a post that explains ssh tunnelling nicely https://superuser.com/questions/96489/ssh-tunnel-via-multiple-hops

Related

Expose API using other server and SSH tunnel

I have been trying to solve this for a couple of days and don't seem to find a way to do it. I have a raspberry pi in my local network which is running jupyter (port 8888) and a flask api (port 5000). I want to be able to access it remotely using another server. My setup and what I have until now is:
Server in GCP with static IP (let's say it's gcp.static.ip). I opened the ports 7003 and 7004 as udp.
Raspberry Pi in my home network with dynamic IP (can't have static IP) and jupyter and flask api on ports 8888 and 5000. I forwarded the ports with:
ssh -NR 7003:localhost:5000 -R 7004:localhost:88888 user#gcp.static.ip
Laptop in remote network. If I do the following ssh tunnel I can access the jupyter server at localhost:7004:
ssh -NL 7004:localhost:7004 user#gcp.static.ip
I can't seem to do the same for the flask API. If I ssh into the gcp server I can query the API at port 7003. How can I set the gcp server so that I can query the api with gcp.static.ip:APIPort and access jupyter in gcp.static.ip:JupyterPort.
Thanks a lot!
UPDATE: I'm able to query the api forwarding a TCP port. However, still want to know if this is possible without having to create another tunnel on my lapto.
Following this Link. Had to change /etc/ssh/sshd_config to set GatewayPorts to clientspecified and ssh tunnel with:
ssh -NR 0.0.0.0:7003:localhost:5000 user#gcp.static.ip

How to find the port number for a remote server?

I'm trying to set up SSH Python interpreter on Pycharm. In the wizard, it is askin for:
Host
Username
Port
I know the the name of my remote workstation and my username. But where can I get the port number from? does it require settings of Pycharm on remote server?
Instructions are not clear.
I have W10 on both sides.
https://www.jetbrains.com/help/pycharm/configuring-remote-interpreters-via-ssh.html
On your Windows 10 remote server you have to enable/install an SSH server. Windows does have a native one, I saw that you can also use OpenSSH now.
And your SSH server should run on port 22 by default.
Try default port 63342 of pycharm besides ssh connection port on 22 :
https://www.jetbrains.com/help/pycharm/settings-debugger.html
You have to setup an SSH server on your remote machine in order to connect to it.
See: https://phoenixnap.com/kb/ssh-to-connect-to-remote-server-linux-or-windows
Once you have done that, the host will be the address of the remote machine, the username and password will be the credentials of a user that exists on the remote machine and the port will be a number that you have chosen when you were setting up the SSH server (22 by default).

PyCharm: Configuring multi-hop remote Interpreters via SSH

To connect to the computer at my office I need to run ssh twice. First to connect to the host-1 and then from host-1 to host-2 and each one has different credentials. However the configuration menu in Pycharm only accepts one ssh tunnel.
Configure Remote Python Interpreter dialog box
Is there any way to set a multi-hop ssh to have access to the interpreter and data files on the host from local?
You can use port forwarding on ssh.
1. Open a terminal and run:
On your local system:
ssh -L 6000:<target_server_ip>:22 <proxy_server_user>#<proxy_server_ip>
You should be connected to the proxy now. You can substitute 6000 with any port.
2. (optional) Test
Now you can ssh into the target server on another terminal with:
ssh -p 6000 <target_server_user>#localhost
3. Configure PyCharm
Keep in mind not to close the first terminal!
The same goes for the PyCharm. Just set the remote interpreter connection through ssh with the following configuration:
host: localhost
port: 6000
user: target_server_user
PyCharm seemingly parses the local .ssh/config too.
If you already have configured ssh hopping there, you can just specify the target server in your pycharm ssh-config.
~/.ssh/config (source)
Host bastion
Hostname bastion.domain.com
Port 2222 # a non-standard port is a good idea
User ironicbadger
Host servera
Hostname servera.lan.local
User servera-user
ProxyCommand ssh bastion -W %h:%p
in pycharm:
Host servera, User name server-user
For users on ssh version 7.3 or later this can be simplified using the ProxyJump parameter.
Host bastion
Hostname bastion.domain.com
User bastion-user
Host servera
Hostname servera.lan.local
User servera-user
ProxyJump bastion

Windows Azure VMs: Connection Timeout to Remote MySQL Database

I'm having a unique problem with Windows Azure that I don't see on other providers. I've been running connections from remote VMs to a MySQL database running on a DigitalOcean VM. I've successfully connected with AWS, Rackspace, Google, and all other providers, but for some reason, Microsoft Azure VMs don't seem to work.
VM OS: Ubuntu 14.04
I'm trying to connect using PyMySQL and SQLAlchemy.
What I've Tried:
The port is open and listening
The user definitely has permission to upload data into the DB (other remote connections with this user all work fine).
I have even tried "ufw disable" for the Firewall on the Windows Azure VM
I've set 3306 as an endpoint on the Azure VM
Despite all my attempts, the connection cannot be established. Is there something I'm missing on the setup?
As Azure VMs disable ICMP and we can use SSH tunnels to allow outside access to internal network resources. However I don’t have resource to create a DigitalOcean VM, but I have created 2 Azure VMs in 2 Cloud Services to try to reproduce the issue.
I installed mysql-server in VM.1 and mysql-client in VM.2.
Then I tried to connect MySQL server directly from VM.2, I got message “can’t connect to MySQL…”.
To work around this issue, I followed this post, created a SSH tunnel in VM.1 which hosted the MySQL server:
Open port 3306, so a remote client can connect to your MySQL Server. Run the following command to open TCP port 3306
iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT
Now let’s check if the port 3306 is open by running this command:
sudo netstat -anltp|grep :3306
Create a SSH tunnel for port 3307
sudo ssh -fNg -L 3307:127.0.0.1:3306 azurevmuser#servername
Create an endpoint for the port 3307 in the dashboard of the VM in Azure management portal. For more details , See how to add endpoint to you Virtual machine. Now your Database host name is <vm_name>.cloudapp.net:3307
Then connect MySQL server from VM.2 using command:
# mysql -h <vm_1_name>.cloudapp.net -P 3307 -u user –pPassword
and it would work fine. Feel free to let us know if we have any misunderstood on your issue.

Cannot connect to python -m SimpleHTTPServer server

this this probably a very simple question, but I haven't been able to find an answer anywhere. On the online articles about it, they didn't show the exact process to share a directory using SimpleHTTPServer. I've run the command successfully and have the server running, but I can only access it on the machine that started it.
192.168.1.2:8000
I've tried it on a Windows machine and iPad (although that doesn't really make a difference) on the local network. To access it, I've been using my local IP address, which I found by running ifconfig | grep inet, which returns (among other matches):
inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255
And after searching a bit online, I found: https://github.com/shbhrsaha/instant-sharing/blob/master/share.py.
There's function which supposedly gives you a handy url to share with your friends, but I tried running locally, and all I got was "localhost.localdomain", which obviously returns 127.0.0.1
How can I make this work?
When you start SimpleHTTPServer it tells which IP addresses it is listening to:
python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
Address 0.0.0.0 means it listening to all available IP addresses. Thus in this case you should simply reach the server by going http://192.168.1.two:8000
If it doesn't work then it is most likely a network issue. You can test this out with telnet command (both Windows and UNIX available): telnet will open a TCP/IP connection on a certain IP and certain port.
E.g. on UNIX you can do:
telnet 192.168.1.2 8000
If you get:
telnet 192.162.1.2 8000
Trying 192.162.1.2...
telnet: connect to address 192.162.1.2: Connection refused
telnet: Unable to connect to remote host
... it means SimpleHTTPServer is running.
If it waits for very long time it means your router/firewall is blocking the connection.
If you get a reply:
telnet localhost 8000
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
... browser should work as well. You can quit telnet by just keep hitting the enter (the SimpleHTTPServer will close the connection).
In Ubuntu
I had the same problem, then I narrowed it down to be a firewall problem.
First, check if your firewall is active.
sudo ufw status
If it is active by default it blocks all incoming connections from anywhere.
You can check whether you have granted access to your running port. The following command will list down all the available rules. if your port is not there with access given to other ports then you need to grant access.
sudo ufw status numbered
[This is what the issue] Now grant access on the port for desired ip addresses/ all. I allowed all incoming connections to the port 8000 on all ip adress by following command.
sudo ufw allow from any to any port 8000 proto udp
Initially, I thought this should be tcp instead of udp but worked with udp only. Something to dig up later.
The server was running, and we could access it, but only on the server's machine.
There was something blocking the connection, and that was The Firewall.
We configured The Firewall and it worked fine, my other device could get access.

Categories