-
Notifications
You must be signed in to change notification settings - Fork 5
/
pptp-vpn-up
141 lines (119 loc) · 3.97 KB
/
pptp-vpn-up
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
#Script, proveryaushii, podnyalos li soedinenie v3
WTIMEOUT=120 #Maksimalnoe vremya ojhidania 's
PEERFILE="/etc/ppp/peers/" #imya peer-file
ADDR="" #adres dlya ping
PING=0
ITEM_ARR=0 #Schetchik dlya tekuschego el-ta massiva
CH_S[0]='-' #Massiv s simvolami 'melnitsy'
CH_S[1]='/'
CH_S[2]='|'
CH_S[3]='\'
#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): "
tput sc #sohranyaem positsiu kursora
while [ $WTIMEOUT -ge 0 ]; do
printf '%3s %s' $WTIMEOUT "${CH_S[ITEM_ARR]}" #vivodim ego i tekushii simvol
tput rc #vosstanavlivaem positsiu kursora
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";
let "ITEM_ARR=ITEM_ARR+1" #uvelichivaem schetchik el-tov
if [ $ITEM_ARR -eq 4 ]; then #esli schetchik = 4
let "ITEM_ARR=0" #obnulyaem ego, chtob opyat' nachat' s 1 el-ta 'melnitsy'
fi
done
# chtob sledujushee soobshenie pechatalos so sledujushei stroki
printf '\n' #perehod na novuju stroku
# Proveryaem, soedinilis ili net i sootvetstvenno soobshaem
if [ $CONNECTED -eq 0 ]
then
echo "Connection timeout over :("
else
echo "Connected sucsesseful!"
fi