Advertisement
tolikpunkoff

getting ip & set default routing

Jan 14th, 2015
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.38 KB | None | 0 0
  1. #!/bin/bash
  2. print_help () {
  3.     echo "use setroute <interface> [svt] [table]";
  4.     echo "<interface> pptp tunnel interface (e.g. ppp0)";
  5.     echo "Options:";
  6.     echo "v - set VIA parameter";
  7.     echo "s - set SRC parameter";
  8.     echo "t - set routing table";
  9.     echo "[table] - routing table (in /etc/iproute2/rt_tables)";
  10.     echo "if the table is not specified, used default route table";
  11. }
  12.  
  13. SETVIA=0
  14. SETSRC=0
  15. ROUTINGTABLE=""
  16.  
  17. #Proveryaem parametry
  18. if [ -z "$1" ]; then #esli 1 opcii net
  19.     print_help #pechataem help i uhodim
  20.     exit 2
  21. fi
  22.  
  23. if [ "$1" = "-h" ] ;then #esli v 1 opcii zapros pomoshi
  24.     print_help #pechataem help i uhodim
  25.     exit 2
  26. fi
  27.  
  28. if [ "$1" = "--help" ] ;then #esli v 1 opcii zapros pomoshi
  29.     print_help #pechataem help i uhodim
  30.     exit 2
  31. fi
  32.  
  33.  
  34. if  echo "$2"| grep -q "v"; then #ustanavlivat li VIA
  35.     SETVIA=1
  36. fi
  37.  
  38. if  echo "$2"| grep -q "s"; then #ustanavlivat li SRC
  39.     SETSRC=1
  40. fi
  41.  
  42. if  echo "$2"| grep -q "t"; then #ustanavlivat li tablicu
  43.     if [ -n $3 ]; then #esli ustanavlivat' proveraem 3 parametr
  44.     if grep -wq "$3" "/etc/iproute2/rt_tables"; then #est' li tablica
  45.         ROUTINGTABLE=$3
  46.     else
  47.         echo "Error! Table $3 not found in rt_tables"
  48.         exit 1
  49.     fi
  50.     fi
  51. fi
  52.  
  53.  
  54. #Proveryaem, podnyat li interfeis
  55. ifconfig $1>/dev/null
  56. if [ "$?" -ne 0 ]; then
  57.     echo "Interface $1 error ";
  58.     exit 1
  59. fi
  60.  
  61. #Izvlekaem IP
  62. TMPGREP=`ifconfig $1|grep "destination"`;#Grep'aem 'destination' (na toi zhe strochke i 'inet')
  63. if [ -z "$TMPGREP" ]; then
  64.     echo "Error! IP data not found!"
  65.     exit 1
  66. fi
  67.  
  68. SRC=`echo $TMPGREP|awk ' {print $2} '`   # S pomoshju awk izvlekaem nujnie
  69. VIA=`echo $TMPGREP|awk ' {print $6} '`   # stolbcy s ip (razdelitel probel)
  70.  
  71.  
  72. # Vivodim summary i formiruem elementy commandy
  73. echo "Device: $1 OK"
  74. if [ $SETSRC -eq 1 ]; then
  75.     echo "SRC set. IP: $SRC"
  76.     SRC="src "$SRC
  77. else
  78.     echo "SRC not set"
  79.     SRC=""
  80. fi
  81.  
  82. if [ $SETVIA -eq 1 ]; then
  83.     echo "VIA set. IP: $VIA"
  84.     VIA="via "$VIA
  85. else
  86.     echo "VIA not set"
  87.     VIA=""
  88. fi
  89.  
  90. if [ -z $ROUTINGTABLE ]; then
  91.     echo "Use default routing table."
  92. else
  93.     echo "Routing table: "$ROUTINGTABLE
  94.     ROUTINGTABLE="table "$ROUTINGTABLE
  95. fi
  96.  
  97. #ustanavlivaem routing
  98. ip route del default $ROUTINGTABLE
  99. ip route add default dev $1 $VIA $SRC $ROUTINGTABLE
  100. if [ $? -eq 0  ]; then
  101.     echo "Routing set!"
  102. else
  103.     echo "Error detected!"
  104. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement