I need to connect to several VNC servers in a configuration like this:
+---------+ |LOCALHOST| +----+----+ | |VPN | +--v--+ |HOST1| +--+--+ | |NETWORK1 | +------v------+ |HOST2 | |VNC 5901:5901| +------+------+ | |NETWORK2 | +------v------+ |HOST3 | |VNC 5902:5901| +-------------+
- Localhost: My laptop running Windows
- Host1: A Linux host which I connect through SSH. This host is connected to the same network than host2.
- Host2: This Linux host is running one of the VNC servers letting port 5901 open. Is connected to host1 using network1 and to host2 using network2. Runs Linux.
- Host3: This Linux host runs the other VNC server, letting port 5901 open. Is connected to host2 using network2. Runs Linux.
So, in order to connect to the VNC servers I'm running some SSH tunnels to redirect traffic in ports 5901 and 5902.
Commands to tunnel look like this:
# Terminal 1 ssh user1@host1 ssh user2@host2 ssh -L 5902:127.0.0.1:5901 -C -N -l user3 host3 -vvv # Terminal 2 ssh user1@host1 ssh -L 5902:127.0.0.1:5902 -C -N -l user2 host2 -vvv # Terminal 3 ssh user1@host1 ssh -nNT -L 5901:host2:5901 user1@host1 -vvv # Terminal 4 ssh -L 5902:127.0.0.1:5902 -C -N -l user1 host1 -vvv # Terminal 5 ssh -nNT -L 5901:host1:5901 user1@host1 -vvv
(Bit of a messy commands, but hey, they work)
In every terminal, this would prompt a message to type the password of the user and will keep a process running until is aborted or a connection timeout happens.
So this works fine and I'm able to connect to VNC servers from my laptop using localhost:5901 and localhost:5902.
The problem is, I have to type a lot of this commands, type the same passwords over and over, opening several terminals that keep open, several times a day (because of connection timeouts), and its getting old real fast.
So I was wondering if this could be automated somehow, the requirement being that whatever script or program could dive deep into several hosts running commands, some of them prompting passwords that needs to be typed, and some of this process will keep running until Ctrl+C is pressed.
I'm asking for names of programs or bash commands that can be used to automate this kind of scenarios so I could research a solution. Thanks.