Last active
August 29, 2015 14:01
-
-
Save jasonwryan/715a4f3fcb55e995de0d to your computer and use it in GitHub Desktop.
Manage SSH proxy tunnels to a number of hosts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# tunnel browser to another box | |
# Copyright (c) 2013 Jason W Ryan | |
# The MIT License (MIT) http://opensource.org/licenses/MIT | |
user="<your_name>" | |
exip="XXX.XXX.XXX.XXX" | |
tunnel_id="$HOME/.local/tmp/tunnel_id" | |
usage () { | |
cat <<EOF | |
tunnel -[x,y,z,d,h] | |
-x | xbox Host_x | |
-y | ybox Host_y | |
-z | zbox Host_z | |
-d | down | |
-h | help print this message | |
EOF | |
} | |
get_host(){ | |
case "$opt" in | |
-x|xbox) octet="10" | |
port="210" | |
;; | |
-y|ybox) octet="20" | |
port="220" | |
;; | |
-z|zbox) octet="30" | |
port="230" | |
;; | |
-d|down) down_tun | |
;; | |
-h|help) usage && exit 1 | |
;; | |
esac | |
# test for LAN | |
testip=$(curl -s icanhazip.com) | |
if [[ $exip == $testip ]]; then | |
SSH_HOST="${user}@192.168.1.${octet} -p ${port}" | |
else | |
SSH_HOST="${user}@${exip} -p ${port}" | |
fi | |
# store the tunnel details | |
mkfifo "$tunnel_id" | |
printf '%s\n' "$SSH_HOST" >"$tunnel_id" & | |
} | |
up_tun(){ | |
ssh -f -N -D 8080 -M -S /tmp/ssh_tunnel_%h.sock -o ExitOnForwardFailure=yes \ | |
$SSH_HOST && \ | |
printf '%s\n' "ssh tunnel started successfully" || \ | |
printf '%s\n' "ssh tunnel failed to start" | |
} | |
down_tun(){ | |
SSH_HOST=$(<"$tunnel_id") | |
ssh -S /tmp/ssh_tunnel_%h.sock -O exit $SSH_HOST 2>/dev/null && \ | |
printf '%s\n' "SSH tunnel closed" | |
# cleanup fifo | |
[[ -p $tunnel_id ]] && rm "$tunnel_id" | |
} | |
run_tun(){ | |
get_host | |
up_tun && chromium --proxy-server="socks://127.0.0.1:8080" &>/dev/null & | |
} | |
opt="$1" | |
if (( $# != 1 )); then | |
printf "%s\n" "Wrong number of parameters…" | |
usage | |
exit 1 | |
else | |
case "$opt" in | |
-d|down) down_tun | |
;; | |
-x|xbox|-y|ybox|-z|zbox) run_tun | |
;; | |
*) usage && exit 1 | |
;; | |
esac | |
fi | |
# vim:set sw=2 ts=2 et: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment