Last active
November 24, 2015 13:14
-
-
Save NiciDieNase/3c93b19bad837df1ea26 to your computer and use it in GitHub Desktop.
SSH-Tunnel/sshuttle-Skript für das Netz der HS Karlsruhe
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
#!/bin/bash | |
# HS-Kürzel | |
HSUSER= | |
# User auf Zielrechner (für sshuttle) | |
REMOTEUSER= | |
# sshuttle-Zielhost | |
SSHUTTLEHOST= | |
# local port for to forward remote port to | |
FORWARDPORT=9222 | |
# pidfile for sshuttle | |
sshuttle_pidfile=/run/sshuttle.pid | |
if [[ -e $sshuttle_pidfile ]]; then | |
sshuttlePID=$(cat $sshuttle_pidfile) | |
fi | |
if [[ -S ~/.ssh/sockets/ssh_tunnel_login.hs-karlsruhe.de.sock ]]; then | |
tunnelPID=$(ssh -S ~/.ssh/sockets/ssh_tunnel_%h.sock -O check [email protected] 2>&1 >/dev/null | sed 's/[^0-9]//g') | |
fi | |
if [[ $1 = "status" ]]; then | |
if [[ -z $tunnelPID ]]; then | |
echo "ssh-tunnel active" | |
else | |
echo "no ssh-tunnel" | |
fi | |
if [[ -n $sshuttlePID ]]; then | |
echo "sshuttle running" | |
else | |
echo "sshuttle not running" | |
fi | |
elif [[ $1 = "stop" ]]; then | |
if [[ -S ~/.ssh/sockets/ssh_tunnel_login.hs-karlsruhe.de.sock ]]; then | |
ssh -S ~/.ssh/sockets/ssh_tunnel_login.hs-karlsruhe.de.sock -O exit [email protected] | |
fi | |
if [[ -n $sshuttlePID ]]; then | |
kill $sshuttlePID | |
fi | |
else | |
if [[ -z $tunnelPID ]]; then | |
if [[ -S ~/.ssh/sockets/ssh_tunnel_login.hs-karlsruhe.de.sock ]]; then | |
ssh -S ~/.ssh/sockets/ssh_tunnel_login.hs-karlsruhe.de.sock -O exit [email protected] | |
fi | |
echo "starting Forwarding" | |
if ssh -f -M -S ~/.ssh/sockets/ssh_tunnel_login.hs-karlsruhe.de.sock -o ExitOnForwardFailure=yes -N -L $FORWARDPORT:$SSHUTTLEHOST:22 [email protected]; then | |
# a=0 | |
while ! ssh -S ~/.ssh/sockets/ssh_tunnel_login.hs-karlsruhe.de.sock -O check [email protected] ; do | |
echo "waiting" | |
# if [[ $a -eq 10]]; then | |
# echo "ssh forwarding timed out" | |
# exit 1 | |
# fi | |
# wait for connection | |
done | |
else | |
echo "forwarding failed" | |
exit 1 | |
fi | |
fi | |
if [[ -e ~/.ssh/sockets/ssh_tunnel_login.hs-karlsruhe.de.sock ]]; then | |
tunnelPID=$(ssh -S ~/.ssh/sockets/ssh_tunnel_%h.sock -O check [email protected] 2>&1 >/dev/null | sed 's/[^0-9]//g') | |
fi | |
if [[ -n $tunnelPID ]]; then | |
if [[ -n $sshuttlePID ]]; then | |
kill $sshuttlePID | |
echo "sshuttle läuft schon, starte neu" | |
else | |
echo "starting Sshuttle" | |
fi | |
touch $sshuttle_pidfile | |
sshuttle -D --pidfile=$sshuttle_pidfile -r $REMOTEUSER@localhost:$FORWARDPORT -x 10.0.0.0/8 -x 127.0.0.0/8 -x 172.16.0.0/12 -x 192.168.0.0/16 -x 193.196.64.0/24 0/0 | |
chmod 644 $sshuttle_pidfile | |
else | |
echo "no ssh-tunnel, can't start sshuttle" | |
exit 2 | |
fi | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Obsolete, works much easier with ProxyCommand