-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute-add
executable file
·98 lines (83 loc) · 1.88 KB
/
route-add
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
#!/bin/bash
trap "exit 1" TERM
export TOP_PID=$$
DOMAIN=$1
GW_DEV=$2
GW_IP=$3
bye()
{
kill -s TERM $TOP_PID
}
askDevice ()
{
while true; do
echo "Select Device:"
mapfile -t dev_list < <(ifconfig -s | cut -f1 -d ' ' | grep -v Iface)
for i in "${!dev_list[@]}"; do
printf "[%d]\t%s\n" "$i" "${dev_list[$i]}"
done
echo "---------------------------------------------"
echo -n "Device number: "
read dev_index
if [ -z "${dev_list[${dev_index}]}" ]; then
echo -e "\n\tInvalid device"
else
GW_DEV=${dev_list[$dev_index]}
break
fi
done
}
getGatewayIp()
{
echo $(ip route sh | grep "$1" | grep "default" | cut -d ' ' -f3)
}
getGatewayMetric()
{
METRIC=""
F8=$(ip route sh | grep "$1" | grep "default" | cut -d ' ' -f8)
if [ "$F8" == "metric" ]; then
METRIC=$(ip route sh | grep "$1" | grep "default" | cut -d ' ' -f9)
fi
echo "$METRIC"
}
getDomainIp()
{
echo $(getent hosts ${DOMAIN} | awk '{print $1}')
}
setDomainRoutes()
{
if [ -z "$4" ]; then
getent hosts $1 | awk '{print $1}' | xargs -i{} sudo ip route replace {} via $2 dev $3
else
getent hosts $1 | awk '{print $1}' | xargs -i{} sudo ip route replace {} via $2 dev $3 metric $4
fi
}
if [ -z "$DOMAIN" ]; then
echo -e "\tDomain is not given!"
bye
fi
DOMAIN_IP=$(getDomainIp)
echo -e "Domain: ${DOMAIN}\t (${DOMAIN_IP})"
if [ -z "$GW_DEV" ]; then
askDevice
fi
echo -e "Device: ${GW_DEV}"
if [ -z "$GW_IP" ]; then
ip=$(getGatewayIp $GW_DEV)
if [ "$ip" == "$GW_DEV" ]; then
echo -e "\tThis is your default out going traffic route!"
echo -e "\tYour route to ${DOMAIN} is already passed via this gateway"
bye
else
GW_IP=${ip}
fi
fi
GW_METRIC=$(getGatewayMetric $GW_DEV)
echo "Gateway IP: ${GW_IP}"
if [ -z "$GW_IP" ]; then
setDomainRoutes ${DOMAIN} ${GW_IP} ${GW_DEV}
else
echo "Gateway Metric: ${GW_METRIC}"
setDomainRoutes ${DOMAIN} ${GW_IP} ${GW_DEV} ${GW_METRIC}
fi
echo "Routes Added."