Skip to content

Instantly share code, notes, and snippets.

@scriptzteam
Forked from egrouse/nzbtunnel.sh
Created January 20, 2017 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scriptzteam/83f839ae69257ac08b448d790c986eb0 to your computer and use it in GitHub Desktop.
Save scriptzteam/83f839ae69257ac08b448d790c986eb0 to your computer and use it in GitHub Desktop.
BASH script to tunnel NNTP connections
#!/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