Guest User

WBWE

a guest
Apr 1st, 2010
1,517
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- /usr/local/bin/vpn --
  2.  
  3. #!/bin/bash
  4.  
  5. PPPD="/usr/sbin/pppd"
  6. SSH="/usr/bin/ssh"
  7. SUDO="/usr/bin/sudo"
  8. USER="$(whoami)"
  9.  
  10. if ! test -f $PPPD  ; then echo -e "\e[1;31mCan't find $PPPD\033[1;0m";  exit; fi
  11. if ! test -f $SSH   ; then echo -e "\e[1;31mCan't find $SSH\033[1;0m" ;  exit; fi
  12. if ! test -f $SUDO  ; then echo -e "\e[1;31mCan't find $SUDO\033[1;0m";  exit; fi
  13. if [ "$(sudo whoami)" != "root" ]; then echo -e "\e[1;31mYou must be in sudoers file!\033[1;0m"; exit; fi
  14. if [ "$SERVER_PORT" = '' ]; then SERVER_PORT="22"; fi
  15.  
  16. function connect {
  17.     printf "\n \e[1;34m::\e[1;37m Connecting to $1:\033[1;0m"
  18.     sudo ${PPPD} updetach noauth passive pty "sudo -u $USER ${SSH} -P ${SERVER_HOSTNAME} -l${SERVER_USERNAME} -p $SERVER_PORT -o Batchmode=yes sudo ${PPPD} nodetach notty noauth" ipparam vpn ${CLIENT_IFIPADDR}:${SERVER_IFIPADDR} 2>&1>/dev/null
  19.     for NET in $NETWORKS; do
  20.         sudo route add -net $NET gw $SERVER_IFIPADDR 2>/dev/null
  21.     done
  22.     if [ "$(ifconfig | grep $CLIENT_IFIPADDR)" != '' ]; then
  23.         PID=`ps ax | grep "${SSH} -P ${SERVER_HOSTNAME} -l${SERVER_USERNAME} -p $SERVER_PORT -o" | grep -v ' passive ' | grep -v 'grep ' | awk '{print $1}'`
  24.         echo "$PID $1" | sudo tee /var/run/vpn.pid > /dev/null
  25.         printf " \e[1;32mConnected\033[1;0m\n\n"
  26.     else
  27.         printf " \e[1;31mConnection Failed!\033[1;0m\n\n"
  28.     fi
  29. }
  30.  
  31. function disconnect {
  32.     if [ -e /var/run/vpn.pid ]; then
  33.         printf "\n \e[1;34m::\e[1;37m Disconnecting from $(cat /var/run/vpn.pid | awk '{print $2}'):\033[1;0m"
  34.         sudo kill $(cat /var/run/vpn.pid | awk '{print $1}')
  35.         sudo rm /var/run/vpn.pid
  36.         printf " \e[1;32mDisconnected\033[1;0m\n"
  37.     else
  38.         printf "\n \e[1;31mFAILED!\033[1;0m\n"
  39.     fi
  40. }
  41.  
  42. case "$1" in
  43.     stop)
  44.         if [ -e /var/run/vpn.pid ]; then
  45.             disconnect
  46.             printf "\n"
  47.         else
  48.             printf "\n \e[1;34m:: \e[1;37mNothing to disconnect\033[1;0m\n\n"
  49.         fi
  50.     ;;
  51.  
  52.     -h)
  53.         echo -e "Usage: vpn ($(echo -e $(ls /etc/vpn/) | sed 's/ /|/g')|stop)"
  54.     ;;
  55.  
  56.     *)
  57.         if [ ! -z "$1" ] && [ -e /etc/vpn/$1 ]; then
  58.             . /etc/vpn/$1
  59.  
  60.             if [ -e /var/run/vpn.pid ] && [ "$(ifconfig | grep ppp)" != '' ]; then
  61.                 printf "\nYou will be disconnected from $(cat /var/run/vpn.pid | awk '{print $2}'). Continue? [y/N] "
  62.                 read KEY
  63.                 if [ "$KEY" = "Y" ] || [ "$KEY" = "y" ]; then
  64.                     disconnect
  65.                     connect $1
  66.                 else
  67.                     echo -e "\nConnection canceled\n"
  68.                 fi
  69.             else
  70.                 connect $1
  71.             fi
  72.         else
  73.             if [ -z "$1" ]; then
  74.                 echo -e "Usage: vpn ($(echo -e $(ls /etc/vpn/) | sed 's/ /|/g')|stop)"
  75.             else
  76.                 echo -e "\nConfiguration "$1" not found\n"
  77.             fi
  78.         fi
  79.     ;;
  80. esac
  81.  
  82. exit 0
  83.  
  84. -- /etc/vpn/esb --
  85. SERVER_HOSTNAME="xxx.xxx.xxx.xxx"
  86. SERVER_USERNAME="user"
  87. SERVER_IFIPADDR="192.168.11.253"
  88. CLIENT_IFIPADDR="192.168.11.250"
  89. SERVER_PORT="22"
  90. NETWORKS="192.168.12.0/24 192.168.13.0/24"
RAW Paste Data