Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- /usr/local/bin/vpn --
- #!/bin/bash
- PPPD="/usr/sbin/pppd"
- SSH="/usr/bin/ssh"
- SUDO="/usr/bin/sudo"
- USER="$(whoami)"
- if ! test -f $PPPD ; then echo -e "\e[1;31mCan't find $PPPD\033[1;0m"; exit; fi
- if ! test -f $SSH ; then echo -e "\e[1;31mCan't find $SSH\033[1;0m" ; exit; fi
- if ! test -f $SUDO ; then echo -e "\e[1;31mCan't find $SUDO\033[1;0m"; exit; fi
- if [ "$(sudo whoami)" != "root" ]; then echo -e "\e[1;31mYou must be in sudoers file!\033[1;0m"; exit; fi
- if [ "$SERVER_PORT" = '' ]; then SERVER_PORT="22"; fi
- function connect {
- printf "\n \e[1;34m::\e[1;37m Connecting to $1:\033[1;0m"
- 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
- for NET in $NETWORKS; do
- sudo route add -net $NET gw $SERVER_IFIPADDR 2>/dev/null
- done
- if [ "$(ifconfig | grep $CLIENT_IFIPADDR)" != '' ]; then
- PID=`ps ax | grep "${SSH} -P ${SERVER_HOSTNAME} -l${SERVER_USERNAME} -p $SERVER_PORT -o" | grep -v ' passive ' | grep -v 'grep ' | awk '{print $1}'`
- echo "$PID $1" | sudo tee /var/run/vpn.pid > /dev/null
- printf " \e[1;32mConnected\033[1;0m\n\n"
- else
- printf " \e[1;31mConnection Failed!\033[1;0m\n\n"
- fi
- }
- function disconnect {
- if [ -e /var/run/vpn.pid ]; then
- printf "\n \e[1;34m::\e[1;37m Disconnecting from $(cat /var/run/vpn.pid | awk '{print $2}'):\033[1;0m"
- sudo kill $(cat /var/run/vpn.pid | awk '{print $1}')
- sudo rm /var/run/vpn.pid
- printf " \e[1;32mDisconnected\033[1;0m\n"
- else
- printf "\n \e[1;31mFAILED!\033[1;0m\n"
- fi
- }
- case "$1" in
- stop)
- if [ -e /var/run/vpn.pid ]; then
- disconnect
- printf "\n"
- else
- printf "\n \e[1;34m:: \e[1;37mNothing to disconnect\033[1;0m\n\n"
- fi
- ;;
- -h)
- echo -e "Usage: vpn ($(echo -e $(ls /etc/vpn/) | sed 's/ /|/g')|stop)"
- ;;
- *)
- if [ ! -z "$1" ] && [ -e /etc/vpn/$1 ]; then
- . /etc/vpn/$1
- if [ -e /var/run/vpn.pid ] && [ "$(ifconfig | grep ppp)" != '' ]; then
- printf "\nYou will be disconnected from $(cat /var/run/vpn.pid | awk '{print $2}'). Continue? [y/N] "
- read KEY
- if [ "$KEY" = "Y" ] || [ "$KEY" = "y" ]; then
- disconnect
- connect $1
- else
- echo -e "\nConnection canceled\n"
- fi
- else
- connect $1
- fi
- else
- if [ -z "$1" ]; then
- echo -e "Usage: vpn ($(echo -e $(ls /etc/vpn/) | sed 's/ /|/g')|stop)"
- else
- echo -e "\nConfiguration "$1" not found\n"
- fi
- fi
- ;;
- esac
- exit 0
- -- /etc/vpn/esb --
- SERVER_HOSTNAME="xxx.xxx.xxx.xxx"
- SERVER_USERNAME="user"
- SERVER_IFIPADDR="192.168.11.253"
- CLIENT_IFIPADDR="192.168.11.250"
- SERVER_PORT="22"
- NETWORKS="192.168.12.0/24 192.168.13.0/24"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement