This script creates an SSH tunnel
with local port forwarding for the specified domain entries. It also updates the /etc/hosts
file and adds an alias with ifconfig
, so that connections to the specified domains are possible.
Tested only on macos
.
To get help for detour.py
simply run python3 detour.py --help
.
In order to get it going, you need 3 things:
- SSH access to the machine
- An input file, containing the host name and port for each domain.
- Sudo access to edit the
/etc/hosts
file and useifconfig
commands.
The input file is a newline separted file containing the destination hosts and their ports, for example a file.txt
.
If no file is given as input, by default the script uses the file.txt
in the local directory.
private.server.com:8443
xyz.server.com:8443
ssh.cool.net:9999
Issue start
to open an SSH tunnel like this:
$ sudo python3 detour.py start -i ./file.txt -s username@myserver.com
The ./file.txt
is the Input File with the specified domain entries.
In order to stop the SSH tunnel and clear up the /etc/hosts
, use stop
:
$ sudo python3 detour.py stop -i ./file.txt -s username@myserver.com
- You need your public key for the SSH connection at
~/.ssh/id_rsa
in order to connect - If you run
start
twice, you'll have to either reboot or kill the first session:# Note the PID of the stale SSH session $ ps aux | grep ssh $ sudo kill <PID of ssh session>
- Add
--k
flag for optionaly specifying public ssh key. - Figure out a way to use
sudo
only for the commands that require it - editing/etc/hosts/
and callingifconfig
. Might need to refactor code in multiple files. - Handle errors more appropriately - e.g. running
start
twice shouldn't crash the app. - Store the ssh socket file
ssh-control-socket
somewhere in/tmp
. - Test on Ubuntu.
- Port to Windows (add checks for
/etc/hosts
andifconfig
commands) - Make the
stop
command not required - e.g. run in interactive mode and kill with CTRL+C. - Add a
status
command that shows more info for the current SSH session - ssh sockets supports this. - Maybe refactor to use Paramiko?
- Use
venv
andsetuptools
to package the app like the Click guys suggest, possibly distribute via pip. - Use a proper color formatting scheme.