Advertisement
tolikpunkoff

up ppp tunnel & show progressbar

Mar 8th, 2016
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.59 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #Script, proveryaushii, podnyalos li soedinenie
  4. WTIMEOUT=120 #Maksimalnoe vremya ojhidania 's
  5. PEERFILE="/etc/ppp/peers/" #imya peer-file
  6. ADDR="" #adres dlya ping
  7. PING=0
  8.  
  9. #Proveryaem parametry
  10. if [ -z "$1" ]; then
  11.     echo "USE myconn <peer file> [-p] [address]";
  12.     echo "Options:";
  13.     echo "<peer file> - file in /etc/ppp/peers";
  14.     echo "[-p] - use ping check";
  15.     echo "[address] - address to ping, default use gateway address";
  16.     exit 2;
  17. fi
  18.  
  19. echo "Connecting to "$1;
  20.  
  21. if [ "$2" = "-p" ]; then #esli vtoroi parametr skripta="-p"
  22.     #budem pingovat
  23.     PING=1; #ustanavlivaem flag vkluchajushii proverku
  24.     echo -n "Ping test address..."
  25.     if [ -n "$3" ]; then #proveryaem zadan li adres
  26.     ADDR=$3; #esli da, sohranyaem v peremennuju
  27.     echo $ADDR;
  28.     else #inache budet ispolzovan adres shluza
  29.     echo "Gateway address.";
  30.     fi
  31. else
  32.     echo "No ping test"
  33. fi
  34.  
  35.  
  36. PEERFILE="$PEERFILE""$1";
  37. #Proveryaem, est li file
  38. if [ ! -e "$PEERFILE" ]; then
  39.     echo "File "$PEERFILE" not found";
  40.     exit 1;
  41. fi
  42.  
  43. #Proveryaem, ne soedineno li uzhe
  44. #(v spiske processov d.b. 2 stroki pppd call NNN, esli soedineno)
  45. TMPGREP=`ps ax|grep "pppd call $1" -c`
  46. if [ $TMPGREP -gt 1 ]; then
  47.     echo "ERROR: $1 already connected"
  48.     exit 2;
  49. fi
  50.  
  51.  
  52. #Ishem pervyi svobodnyi nomer ppp soedinenia (eto nado dalshe)
  53. FIRSTFREE=0;
  54. ifconfig ppp$FIRSTFREE >/dev/null 2>/dev/null
  55. while [ $? -eq 0 ]; do
  56.     let "FIRSTFREE = FIRSTFREE+1";
  57.     ifconfig ppp$FIRSTFREE >/dev/null 2>/dev/null
  58. done
  59.  
  60.  
  61. #ishem parameter unit i udalyaem lishnee
  62. PPPUNIT=`grep "unit" $PEERFILE`;
  63. PPPUNIT=${PPPUNIT//unit/}
  64. PPPUNIT=${PPPUNIT// /}
  65.  
  66. if [ ! -n "$PPPUNIT" ]; then   
  67.     #Esli parametr unit ne ukazan, sistema ispolzuet pervy svobodny
  68.     #pri podnyatii soedinenia
  69.    
  70.     echo "Parameter 'unit' not found, system use first free ($FIRSTFREE)";
  71.     PPPUNIT=FIRSTFREE;
  72. else
  73.     #Esli ukazan, to nado proverit' ne zanyat li on
  74.     ifconfig ppp$PPPUNIT >/dev/null 2>/dev/null
  75.     if [ $? -eq 0 ]; then       #zanyat
  76.     echo "WARNING! Unit #$PPPUNIT is already in use, system use
  77.     first free ($FIRSTFREE)";
  78.     PPPUNIT=FIRSTFREE;
  79.     else            #svoboden
  80.     echo "Unit $PPPUNIT is free. OK."
  81.     fi
  82. fi
  83.  
  84. #ustanavlivaem soedinenie
  85. pppd call $1
  86.  
  87. CONNECTED=0 #flag, podnyalos li soedinenie
  88.  
  89. echo -n "Wait ("$WTIMEOUT" secounds): "
  90. while [ $WTIMEOUT -ge 0 ]; do
  91.      echo -n ".";
  92.      sleep 1
  93.     #***************************************************
  94.     TMPGREP=`ifconfig ppp$PPPUNIT 2>/dev/null|grep "destination"` #pytaemsa poluchit strochku s ip shluza
  95.     if [ -n "$TMPGREP" ]; then #esli strochka est'
  96.     TMPGREP=`echo $TMPGREP|awk ' {print $6} '` #izvlekaem awk IP shluza
  97.     if [ -n "$TMPGREP" ];then #esli adres poluchen
  98.         if [ $PING -eq 1 ]; then #esli nado pingovat'
  99.         if [ -z "$ADDR" ]; then #adres ne zadan, ispolzuem shluz
  100.                 ADDR=$TMPGREP
  101.             fi
  102.  
  103.         ping -c 1 -I ppp$PPPUNIT $ADDR >/dev/null 2>/dev/null #pytaemsya propingovat shluz
  104.             if [ $? -eq 0 ]; then # esli poluchilos
  105.                 CONNECTED=1 #ustanavlivaem flag soedinenija
  106.                 break #i preryvaem cykl. Ura!!
  107.             fi
  108.             else #esli pingovat' ne nado
  109.             CONNECTED=1 #ustanavlivaem flag soedinenija
  110.             break #i preryvaem cykl. Ura!!
  111.             fi
  112.         fi
  113.     fi
  114.     #***************************************************
  115.      let "WTIMEOUT=WTIMEOUT-1";
  116. done
  117.  
  118. # chtob sledujushee soobshenie pechatalos so sledujushei stroki
  119. echo
  120.  
  121. # Proveryaem, soedinilis ili net i sootvetstvenno soobshaem
  122. if [ $CONNECTED -eq 0 ]
  123. then
  124.      echo "Connection timeout over :("
  125. else
  126.      echo "Connected sucsesseful!"
  127. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement