Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Script, proveryaushii, podnyalos li soedinenie
- WTIMEOUT=120 #Maksimalnoe vremya ojhidania 's
- PEERFILE="/etc/ppp/peers/" #imya peer-file
- ADDR="" #adres dlya ping
- PING=0
- #Proveryaem parametry
- if [ -z "$1" ]; then
- echo "USE myconn <peer file> [-p] [address]";
- echo "Options:";
- echo "<peer file> - file in /etc/ppp/peers";
- echo "[-p] - use ping check";
- echo "[address] - address to ping, default use gateway address";
- exit 2;
- fi
- echo "Connecting to "$1;
- if [ "$2" = "-p" ]; then #esli vtoroi parametr skripta="-p"
- #budem pingovat
- PING=1; #ustanavlivaem flag vkluchajushii proverku
- echo -n "Ping test address..."
- if [ -n "$3" ]; then #proveryaem zadan li adres
- ADDR=$3; #esli da, sohranyaem v peremennuju
- echo $ADDR;
- else #inache budet ispolzovan adres shluza
- echo "Gateway address.";
- fi
- else
- echo "No ping test"
- fi
- PEERFILE="$PEERFILE""$1";
- #Proveryaem, est li file
- if [ ! -e "$PEERFILE" ]; then
- echo "File "$PEERFILE" not found";
- exit 1;
- fi
- #Proveryaem, ne soedineno li uzhe
- #(v spiske processov d.b. 2 stroki pppd call NNN, esli soedineno)
- TMPGREP=`ps ax|grep "pppd call $1" -c`
- if [ $TMPGREP -gt 1 ]; then
- echo "ERROR: $1 already connected"
- exit 2;
- fi
- #Ishem pervyi svobodnyi nomer ppp soedinenia (eto nado dalshe)
- FIRSTFREE=0;
- ifconfig ppp$FIRSTFREE >/dev/null 2>/dev/null
- while [ $? -eq 0 ]; do
- let "FIRSTFREE = FIRSTFREE+1";
- ifconfig ppp$FIRSTFREE >/dev/null 2>/dev/null
- done
- #ishem parameter unit i udalyaem lishnee
- PPPUNIT=`grep "unit" $PEERFILE`;
- PPPUNIT=${PPPUNIT//unit/}
- PPPUNIT=${PPPUNIT// /}
- if [ ! -n "$PPPUNIT" ]; then
- #Esli parametr unit ne ukazan, sistema ispolzuet pervy svobodny
- #pri podnyatii soedinenia
- echo "Parameter 'unit' not found, system use first free ($FIRSTFREE)";
- PPPUNIT=FIRSTFREE;
- else
- #Esli ukazan, to nado proverit' ne zanyat li on
- ifconfig ppp$PPPUNIT >/dev/null 2>/dev/null
- if [ $? -eq 0 ]; then #zanyat
- echo "WARNING! Unit #$PPPUNIT is already in use, system use
- first free ($FIRSTFREE)";
- PPPUNIT=FIRSTFREE;
- else #svoboden
- echo "Unit $PPPUNIT is free. OK."
- fi
- fi
- #ustanavlivaem soedinenie
- pppd call $1
- CONNECTED=0 #flag, podnyalos li soedinenie
- echo -n "Wait ("$WTIMEOUT" secounds): "
- while [ $WTIMEOUT -ge 0 ]; do
- echo -n ".";
- sleep 1
- #***************************************************
- TMPGREP=`ifconfig ppp$PPPUNIT 2>/dev/null|grep "destination"` #pytaemsa poluchit strochku s ip shluza
- if [ -n "$TMPGREP" ]; then #esli strochka est'
- TMPGREP=`echo $TMPGREP|awk ' {print $6} '` #izvlekaem awk IP shluza
- if [ -n "$TMPGREP" ];then #esli adres poluchen
- if [ $PING -eq 1 ]; then #esli nado pingovat'
- if [ -z "$ADDR" ]; then #adres ne zadan, ispolzuem shluz
- ADDR=$TMPGREP
- fi
- ping -c 1 -I ppp$PPPUNIT $ADDR >/dev/null 2>/dev/null #pytaemsya propingovat shluz
- if [ $? -eq 0 ]; then # esli poluchilos
- CONNECTED=1 #ustanavlivaem flag soedinenija
- break #i preryvaem cykl. Ura!!
- fi
- else #esli pingovat' ne nado
- CONNECTED=1 #ustanavlivaem flag soedinenija
- break #i preryvaem cykl. Ura!!
- fi
- fi
- fi
- #***************************************************
- let "WTIMEOUT=WTIMEOUT-1";
- done
- # chtob sledujushee soobshenie pechatalos so sledujushei stroki
- echo
- # Proveryaem, soedinilis ili net i sootvetstvenno soobshaem
- if [ $CONNECTED -eq 0 ]
- then
- echo "Connection timeout over :("
- else
- echo "Connected sucsesseful!"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement