Advertisement
tolikpunkoff

up ppp tunnel & show progressbar v3

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