Created
February 8, 2012 20:11
-
-
Save egrouse/1772949 to your computer and use it in GitHub Desktop.
BASH script to tunnel NNTP connections
This file contains 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 | |
# Script that attempts to create a tunnel for Usenet (NNTP) to pass through | |
# This script: creates a tunnel, assigns the host address to local address in /etc/hosts | |
# For Mac OS X | |
### CONFIGURATION ### | |
SERVER='your.nntp.server' | |
PORT='your.nntp.port' | |
LOCAL='127.0.0.1' | |
USER='root' | |
HOST='your.ssh.server' | |
### END CONFIGURATION ### | |
### NOTHING BELOW THIS LINE NEEDS EDITING ### | |
USER=`whoami` | |
if [ $USER != 'root' ] | |
then | |
echo "Sorry - this script must be run as root to function" | |
exit | |
elif [ -z $1 ] | |
then | |
echo "Usage: nzbtunnel {start|stop}" | |
fi | |
GREPSTR='# Entry added for NNTP Tunnel' | |
case "$1" in | |
start) | |
# Check if the pid file already exists | |
if [ -a /tmp/nzbtunnel.pid ] | |
then | |
echo "PID file exists - tunnel is already running?" | |
exit | |
fi | |
# Open the tunnel | |
CMSTR="ssh -f $USER@$HOST -L $PORT:$SERVER:$PORT -N" | |
$CMSTR | |
# Add the entry into /etc/hosts | |
echo '' >> /etc/hosts | |
echo "$GREPSTR" >> /etc/hosts | |
echo $LOCAL $SERVER >> /etc/hosts | |
# Flush the DNS cache | |
dscacheutil -flushcache | |
# Save PID | |
PID=`ps aux | grep "$CMSTR" | awk '{print $2}'` | |
for a in $PID; do | |
echo $a >> /tmp/nzbtunnel.pid | |
done | |
echo "Tunnel is now running" | |
;; | |
stop) | |
# Try and stop the tunnel | |
for pid in `cat /tmp/nzbtunnel.pid`; do | |
kill -9 $pid &> /dev/null | |
done | |
# Find which line we added text to | |
LINE=`grep "$GREPSTR" /etc/hosts -n | tr ':' '\n' | head -n 1` | |
LINE=`echo $LINE-1 | bc` | |
if [ $LINE != '-1' ] | |
then | |
NEW=`head -n $LINE /etc/hosts` | |
echo "$NEW" > /etc/hosts | |
fi | |
# Flush the DNS cache | |
dscacheutil -flushcache | |
# Delete the pid file | |
rm /tmp/nzbtunnel.pid | |
echo "Tunnel shut down" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment