-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetroute
104 lines (87 loc) · 2.28 KB
/
setroute
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
#!/bin/bash
print_help () {
echo "use setroute <interface> [svt] [table]";
echo "<interface> pptp tunnel interface (e.g. ppp0)";
echo "Options:";
echo "v - set VIA parameter";
echo "s - set SRC parameter";
echo "t - set routing table";
echo "[table] - routing table (in /etc/iproute2/rt_tables)";
echo "if the table is not specified, used default route table";
}
SETVIA=0
SETSRC=0
ROUTINGTABLE=""
#Proveryaem parametry
if [ -z "$1" ]; then #esli 1 opcii net
print_help #pechataem help i uhodim
exit 2
fi
if [ "$1" = "-h" ] ;then #esli v 1 opcii zapros pomoshi
print_help #pechataem help i uhodim
exit 2
fi
if [ "$1" = "--help" ] ;then #esli v 1 opcii zapros pomoshi
print_help #pechataem help i uhodim
exit 2
fi
if echo "$2"| grep -q "v"; then #ustanavlivat li VIA
SETVIA=1
fi
if echo "$2"| grep -q "s"; then #ustanavlivat li SRC
SETSRC=1
fi
if echo "$2"| grep -q "t"; then #ustanavlivat li tablicu
if [ -n $3 ]; then #esli ustanavlivat' proveraem 3 parametr
if grep -wq "$3" "/etc/iproute2/rt_tables"; then #est' li tablica
ROUTINGTABLE=$3
else
echo "Error! Table $3 not found in rt_tables"
exit 1
fi
fi
fi
#Proveryaem, podnyat li interfeis
ifconfig $1>/dev/null
if [ "$?" -ne 0 ]; then
echo "Interface $1 error ";
exit 1
fi
#Izvlekaem IP
TMPGREP=`ifconfig $1|grep "destination"`;#Grep'aem 'destination' (na toi zhe strochke i 'inet')
if [ -z "$TMPGREP" ]; then
echo "Error! IP data not found!"
exit 1
fi
SRC=`echo $TMPGREP|awk ' {print $2} '` # S pomoshju awk izvlekaem nujnie
VIA=`echo $TMPGREP|awk ' {print $6} '` # stolbcy s ip (razdelitel probel)
# Vivodim summary i formiruem elementy commandy
echo "Device: $1 OK"
if [ $SETSRC -eq 1 ]; then
echo "SRC set. IP: $SRC"
SRC="src "$SRC
else
echo "SRC not set"
SRC=""
fi
if [ $SETVIA -eq 1 ]; then
echo "VIA set. IP: $VIA"
VIA="via "$VIA
else
echo "VIA not set"
VIA=""
fi
if [ -z $ROUTINGTABLE ]; then
echo "Use default routing table."
else
echo "Routing table: "$ROUTINGTABLE
ROUTINGTABLE="table "$ROUTINGTABLE
fi
#ustanavlivaem routing
ip route del default $ROUTINGTABLE
ip route add default dev $1 $VIA $SRC $ROUTINGTABLE
if [ $? -eq 0 ]; then
echo "Routing set!"
else
echo "Error detected!"
fi