Last active
August 29, 2015 14:02
-
-
Save sctfic/0854c417ebfdd54d1a5c to your computer and use it in GitHub Desktop.
Service to open ssh connection
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 | |
# DESCRIPTION | |
# Connect ssh as STunnel | |
# | |
# USAGE | |
# sudo tunnel-ssh start[ username@server[:port]] | |
# sudo tunnel-ssh restart[ username@server[:port]] | |
# sudo tunnel-ssh stop | |
# sudo tunnel-ssh status | |
# | |
# AUTHOR | |
# [email protected] | |
### BEGIN INIT INFO | |
# Provides: tunnel-ssh | |
# Required-Start: $local_fs $remote_fs $network $syslog $named | |
# Required-Stop: $local_fs $remote_fs $network $syslog $named | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Start/stop Transparent tunnel server for VPN over SSH | |
# Description: Provide a wrapper around sshuttle | |
### END INIT INFO | |
name="tunnel-ssh" | |
ssh_conf="$2" | |
# user="${ssh_conf%%@*}" | |
# server="${ssh_conf##*@}" | |
# host="${server%%:*}" | |
# port=${server##*:} | |
# port=${port:-22} | |
whoAmI="coaxis" # user running the service | |
pidFile="/tmp/$name/$ssh_conf" | |
mkdir -p "$(dirname "$pidFile")" | |
# Arrêt du service | |
tunnel_stop() { | |
if [[ -f "$pidFile" ]] ; then | |
echo "Stopping $name..." | |
sudo -u "$whoAmI" kill -9 $(cat "$pidFile") | |
sudo -u "$whoAmI" rm "$pidFile" && echo "OK" | |
fi | |
} | |
# Démarrage du service | |
tunnel_start() { | |
# malformed ssh address | |
# [[ "$ssh_conf" != *@* || -z "$user" || -z "$host" ]] && tunnel_usage | |
grep -q "Host $ssh_conf" "/home/coaxis/.ssh/config" || tunnel_usage | |
# if grep -q "Host $ssh_conf" "/home/coaxis/.ssh/config"; then | |
# tunnel_usage | |
# fi | |
#-e 'ssh -i '$ssh_identity | |
echo "Starting $name..." | |
args=( | |
-v # super verbose | |
) | |
sudo -u "$whoAmI" ssh "$ssh_conf" "${args[@]}" & echo $! > "$pidFile" | |
# tunnel_status | |
} | |
tunnel_status() { | |
PID=$(tunnel_pid) || true | |
if [ -n "$PID" ]; then | |
echo "$name to $ssh_conf is running (pid $PID)." | |
exit 0 | |
else | |
echo "$name to $ssh_conf is NOT running." | |
if [ -e "$PIDFILE" ]; then | |
exit 1 | |
else | |
exit 3 | |
fi | |
fi | |
} | |
# TODO: fake it as I'm not aware a config file to 'reload' | |
tunnel_reload() { | |
tunnel_stop | |
tunnel_start | |
} | |
tunnel_usage() { | |
echo "Usage: $(basename $0) start|restart[ username@server[:port]]|stop|status " | |
exit 1 | |
} | |
tunnel_pid() { | |
# if there is actually an apache2 process whose pid is in pidFile, | |
# print it and return 0. | |
if [ -e "$pidFile" ]; then | |
if pidof "python" | tr ' ' '\n' | grep -w $(cat $pidFile); then | |
return 0 | |
fi | |
fi | |
return 1 | |
} | |
# commandes | |
case $1 in | |
start) | |
tunnel_start | |
;; | |
stop) | |
tunnel_stop | |
;; | |
restart) | |
tunnel_stop | |
tunnel_start | |
;; | |
reload | force-reload | graceful) | |
tunnel_reload | |
;; | |
status) | |
tunnel_status | |
;; | |
*) | |
tunnel_usage | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment