Advertisement
Guest User

Untitled

a guest
Jan 6th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.81 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if [ $# != 1 ] ; then
  4.   echo "Usage: (sudo) sh $0 {start|stop}"
  5.   exit 1;
  6. fi
  7.  
  8. IFACE=wlp32s0
  9.  
  10. function getIP(){
  11.   ip addr show $1 | grep -v "127" | grep "inet " | awk '{print $2}' | sed 's:/.*::'
  12. }
  13.  
  14. function getppp0IP(){
  15.   ip addr show ppp0 | grep -Po "peer.*/32" | grep -Po "[0-9.]+" | grep -Po -m1 "[0-9.]+"
  16. }
  17.  
  18. function getVPN_ADDR(){
  19.   ping -c 1 usil-sfe.pointtoserver.com | grep -Eom1 "[0-9.]{3,}" | sed 's/\s//g'    
  20. }
  21.  
  22. function getGateWay(){
  23.   ip route list | grep -Eom1 "via ([0-9.]{11})" | grep -Eom1 "[0-9.]{11}"
  24. }
  25. function getVPNGateWay(){
  26.   ip route list | grep -m1 "$VPN_ADDR" | grep -Po "via .*" | grep -Po "[1-9.]+" | head -n 1
  27. }
  28.  
  29. GW_ADDR=$(getGateWay)
  30. #GW_ADDR="192.168.1.1"
  31. VPN_ADDR=$(getVPN_ADDR)
  32.  
  33. function start(){
  34.   sed -i "s/^lns =.*/lns = $VPN_ADDR/g" /etc/xl2tpd/xl2tpd.conf
  35.   sed -i "s/plutoopts=.*/plutoopts=\"--interface=$IFACE\"/g" /etc/ipsec.conf
  36.   sed -i "s/left=.*$/left=$(getIP $IFACE)/g" /etc/ipsec.conf
  37.   sed -i "s/right=.*$/right=$VPN_ADDR/g" /etc/ipsec.conf
  38.   sed -i "s/^.*: PSK/$(getIP $IFACE) $VPN_ADDR : PSK/g" /etc/ipsec.secrets
  39.   systemctl start openswan
  40.   sleep 2    #delay to ensure that IPsec is started before overlaying L2TP
  41.  
  42.   systemctl start xl2tpd
  43.   ipsec auto --up pure-NY                        
  44.   echo "c vpn-connection" > /var/run/xl2tpd/l2tp-control    
  45.   sleep 2    #delay again to make that the PPP connection is up.
  46.  
  47.   ip route save 1> $HOME/.cache/ipdump
  48.   ip route add $VPN_ADDR via $GW_ADDR dev $IFACE
  49. }
  50.  
  51. function stop(){
  52.   VPN_GW=$(getVPNGateWay)
  53.   ip route delete $VPN_ADDR via $GW_ADDR dev $IFACE
  54.   ip route restore < $HOME/.cache/ipdump
  55.   rm $HOME/.cache/ipdump
  56.  
  57.   ipsec auto --down pure-NY
  58.   /bin/echo "d vpn-connection" > /var/run/xl2tpd/l2tp-control
  59.   systemctl stop xl2tpd
  60.   systemctl stop openswan
  61. }
  62.  
  63. $1
  64. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement