Wednesday, November 03, 2010

IPIP tunnel creator via bash

   1:  #!/bin/bash
   2:  #
   3:  # Tunnel ipip Automation
   4:  # Creator: Shiran Guez
   5:  #
   6:  TUN_ETH="eth0"
   7:  IP_DEST_EXT=""
   8:  TUNNEL_IP_SRC=""
   9:  TUNNEL_IP_DST=""
  10:  _CREATE_ () {
  11:  echo "Please enter the name of your local interface [eth0 is default]: "
  12:  echo "[ To Skip enter >>>skip<<< ]"
  13:  while :;
  14:  do
  15:      read COMM_ETH
  16:      if [ "$COMM_ETH" == "skip" ] ;then 
  17:          break
  18:      elif [ "$COMM_ETH" == "eth0" ] ;then
  19:          break
  20:      else
  21:          ifconfig "$COMM_ETH" >/dev/null
  22:          if [ $? -eq 0 ] ;then
  23:              TUN_ETH="$COMM_ETH"
  24:              break
  25:          else
  26:              echo "You have entered a wrong ethernet interface"
  27:              echo "Please try again: "
  28:          fi
  29:      fi
  30:   
  31:  done
  32:  localip=$(ifconfig $TUN_ETH | grep Bcast | awk '{print $2}' | awk -F: '{print $2}')
  33:  echo "Please enter the destination IP for the tunnel: "
  34:  while :;
  35:  do
  36:      read IP_DEST_EXT
  37:      echo "Are you sure $IP_DEST_EXT is a valid IP [Y]:"
  38:      read ANS_ME
  39:      ANS_ME=$(echo $ANS_ME | tr a-z A-Z)
  40:      if [ "$ANS_ME" == "Y" ] ;then
  41:          break
  42:      else
  43:          echo "Please enter destination again:"
  44:       fi
  45:  done       
  46:  echo "Please enter tunnel IP and Peer Address: "
  47:  while :;
  48:  do  
  49:      echo "IP : "    
  50:          read TUNNEL_IP_SRC
  51:      echo "Peer : "
  52:      read TUNNEL_IP_DST
  53:      echo "Peer subnet mask [ /16 , /24 ...]: "
  54:      read TUNNEL_IP_DST_MASK
  55:      echo "Tunnel IP will be $TUNNEL_IP_SRC and its peer address will be $TUNNEL_IP_DST using subnet mask $TUNNEL_IP_DST_MASK"
  56:      echo "Please confirm [Y]:"
  57:          read ANS_ME_NOW
  58:          ANS_ME_NOW=$(echo $ANS_ME_NOW | tr a-z A-Z)
  59:          if [ "$ANS_ME_NOW" == "Y" ] ;then
  60:                  break
  61:          else
  62:                  echo "Please enter again:"
  63:          fi
  64:   
  65:  done
  66:  ip tu ad ipiptun mode ipip local $localip remote $IP_DEST_EXT ttl 64 dev $TUN_ETH
  67:  ip ad ad dev ipiptun $TUNNEL_IP_SRC peer $TUNNEL_IP_DST/$TUNNEL_IP_DST_MASK
  68:  ifconfig ipiptun up
  69:  }
  70:   
  71:  _DESTROY_ () {
  72:  ip add | grep ipiptun > /dev/null
  73:  if [ $? -eq 0 ] ;then
  74:      ifconfig ipiptun down
  75:      echo "Tunnel was removed"
  76:  else
  77:      echo "No Tunnel to remove"
  78:  fi
  79:  }
  80:   
  81:  lsmod | grep ipip > /dev/null
  82:  if [ $? -eq 0 ] ;then
  83:      while :;
  84:      do
  85:          echo "Please enter 1 to create or 2 for removing a tunnel"
  86:          read SELECTION
  87:          if [ "$SELECTION" == "1" ] ; then
  88:              _CREATE_
  89:              break
  90:          elif [ "$SELECTION" == "2" ] ; then
  91:              _DESTROY_
  92:              break
  93:          fi
  94:      
  95:      done
  96:  else
  97:      echo "ipip module is not loaded try to first load the ipip module and then run the tool again, Thank you!"
  98:  fi

No comments: