How to connect to expo via private tunnel (not ngrok) - ssh

I have the problem that at work I can not connect via network to expo, so I need to use tunnel, which is fine. However sometimes the tunnel is really slow destroying any developer expierience.
Since I can also host expo locally on localhost I had the idea of simply ssh-tunneling to a remote server that has an open port.
my remote host runs ubuntu
so i SSH there like so:
ssh -R 0.0.0.0:19000:0.0.0.0:19000 user#ip
in order for this to work i also added
GatewayPorts clientspecified
to my /etc/ssh/sshd_config
...
sudo netstat -plutn
shows me
tcp 0 0 0.0.0.0:19000 0.0.0.0:* LISTEN 20183/2
so accepting requests (i also tried to forward port 19001 to get something back when i enter it in the browser which worke fine)
However when i enter:
exp://serverip:19000 into the expo client on my android phone he can't connect.
Any ideas on help?

It looks like Expo uses multiple ports 19000, 19001, and 19002. So you will need to forward all of these.
e.g.
$ ssh -f -N -R 19000:localhost:19000 user#ip
$ ssh -f -N -R 19001:localhost:19001 user#ip
$ ssh -f -N -R 19002:localhost:19002 user#ip
Also, you can set the REACT_NATIVE_PACKAGER_HOSTNAME environment variable to use the remote host.
$ export REACT_NATIVE_PACKAGER_HOSTNAME="ip"
$ expo start

Related

Autokill broken reverse ssh tunnels

I have 1 server which is behind a NAT and a firewall and I have another in another location that is accessible via a domain. The server behind the NAT and firewall is running on a cloud environment and is designed to be disposable ie if it breaks we can simply redeploy it with a single script, in this case, it is OpenStack using a heat template. When that server fires up it runs the following command to create a reverse SSH tunnel to the server outside the NAT and Firewall to allow us to connect via port 8080 on that server. The issue I am having is it seems if that OpenSSH tunnel gets broken (server goes down maybe) the tunnel remains, meaning when we re-deploy the heat template to launch the server again it will no longer be able to connect to that port unless I kill the ssh process on the server outside the NAT beforehand.
here is the command I am using currently to start the reverse tunnel:
sudo ssh -f -N -T -R 9090:localhost:80 user#example.com
I had a similar issue, and fixed it this way:
First, at the server, I created in the home directory a script called .kill_tunel_ssh.sh with this contents:
#this finds the process that is opening the port 9090, finds its PID and kills it
sudo netstat -ltpun | grep 9090 | grep 127 | awk -F ' ' '{print $7}' | awk -F '/' '{print $1}' | xargs kill -9
Then, at the client, I created a script called connect_ssh.sh with this contents:
#this opens a ssh connection, runs the script .kill_tunnel_ssh.sh and exit
ssh user#remote.com "./.kill_tunel_ssh.sh"
#this opens a ssh connection opening the reverse tunnel
ssh user#remote.com -R 9090:localhost:80
Now, I always use connect_ssh.sh to open the SSH connection, instead of using the ssh command directly.
It requires the user at the remote host to have sudo configured without asking for password when executing the netstat command.
Maybe (probably) there is a better way to accomplish it, but that is working for me.

Creating SSH tunnel without running the ssh command

Establishing SSH tunnel can done from the command line by explicitly giving
ssh -N -f -L 18888:192.168.224.143:8888 username#192.168.224.143
or defining tunnel in ~/.ssh/config file
Host tunnel
HostName 192.168.224.143
IdentityFile ~/.ssh/mine.key
LocalForward 18888 192.168.224.143:8888
User username
and then running,
ssh -f -N tunnel
Is there a way to start this tunnel without running the ssh ssh -f -N tunnel command explicitly?
I would like to establish this tunnel whenever my machine boots up. Do not want to add it in init script. Can it be done with SSH configuration itself?
No. SSH configuration is not designed to start something for you automatically. You need to add it to your startup applications or init script/systemd service, if you want to start it automatically after the network.
I also recommend you to use autossh which will take care of re-establishing the tunnel, if it fails for some reason.

Debug port forwarding for using Jupyter notebook remotely

I am trying to use Jupyter notebook on a remote computer. The setup is as follows: I have my home laptop, which can ssh to a specific computer on my university's network (e.g., gateway.myuniv.edu). Once I am logged to gateway.myuniv.edu, I can ssh to the computer on which I would like to run the Jupyter notebook server (e.g. cluster.myuniv.edu).
What works: I can run the server on the gateway and connect to it from my laptop using local port forwarding, as follows:
On gateway.myuniv.edu: $ jupyter notebook --no-browser --port 8888
On my laptop: $ ssh -v -N -L 9000:localhost:8888 myusername#gateway.myuniv.edu
Then on my laptop's browser, I open the url: http://localhost:9000
What doesn't work: I don't want to run the server on the gateway, since I can't do heavy computations there. I tried to do the following:
On cluster.myuniv.edu: $ jupyter notebook --no-browser --port 8888
On my laptop: $ ssh -v -N -L 9000:cluster.myuniv.edu:8888 myusername#gateway.myuniv.edu
Then on my laptop's browser, I open the url: http://localhost:9000. This doesn't work: SSH says that the connection is refused.
I don't understand why this would happen and how to debug this, would be happy for any help. Thanks!
The issue is that you are forwarding port :8888 on cluster.myuniv.edu to port :9000 on gateway.myuniv.edu and then forwarding port :8888 on gateway.myuniv.edu to port 9000 on your laptop.
The solution would the following:
On cluster.myuniv.edu: $ jupyter notebook --no-browser --port 8888
On gateway.myuniv.edu: $ ssh -v -N -L 8888:localhost:8888 myusername#cluster.myuniv.edu
On laptop: $ ssh -v -N -L 9000:localhost:8888 myusername#gateway.myuniv.edu
I would also recommend that you run Jupyter notebook (on the cluster) and the ssh tunneling (on the gateway) using Tmux or Screen so it remains active even if you close terminal
You can use gateway.myuniv.edu as a Jump (ssh -J) host.
So start jupyter notebook on your cluster as you normally do:
jupyter notebook --no-browser --port 8888
On your local laptop: ssh username#cluster.myuniv.edu -J username#gateway.myuniv.edu -L 8888:localhost:8888 -N

Tunelling VNC through two ssh hops

I've long seeked a solution to tunnel to a machine behind a firewall, passing VNC (or other ports) through. Like explained in this old usenet post, which I'll recap here:
I have to log through an intermediate machine, something like:
local $ ssh interim
interim $ ssh remote
remote $ ...any commands...
This works fine. But now I am trying to tunnel a vnc session from remote to local and I can't find the magic incantation, using either one or two steps.
I recently found a wonderfully simple and adaptable solution: simply tunnel the ssh to the target system through the connection to the firewall. Like such:
local $ ssh -L 2222:remote:22 interim
interim $ ...no need to do anything here...
In another local console you connect to localhost on port 2222, which is actually your remote destination:
local $ ssh -C -p 2222 -L 5900:localhost:5900 localhost
remote $ ...possibly start you VNC server here...
In yet another local console:
local $ xtightvncviewer :0
It's that simple. You can add any port forwarding you want to the 2nd command (-L localport:localhost:remoteport) just like if there wasn't any intermediate firewall. For instance for RDP: -L 3389:localhost:3389

How to connect to production machine using SSH

How do I connect to a production machine using SSH tunnel? It has few blocked ports which I would like to connect from my development box and debug it.
Use the following command
ssh -N -v -L<LOCAL_PORT>:<PRODUCTION_MACHINE>:<PRODUCTION_PORT> <PRODUCTION_MACHINE>
E.g) ssh -N -v -L2047:my-production-server.com:8000 my-production-server.com