forked from Xentrk/x3mRouting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x3mRouting_client_config.sh
160 lines (134 loc) · 7.85 KB
/
x3mRouting_client_config.sh
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/sh
####################################################################################################
# Script: x3mRouting_client_config.sh
# VERSION=2.0.1
# Author: Xentrk
# 2-February-2020
#
#####################################################################################################
# Description:
#
# Create the file x3mRouting_client_rules in the x3mRouting directory. The file contains a separate line for each for each LAN
# client with a static IP assignment. Each record contains three fields separated by a space. The first
# field is number representing the interface assignment, followed by the LAN client IP address and
# description. By default, each LAN client is assigned to the OVPNC1 interface. The user must edit
# x3mRouting_client_rules and assign the interface for each LAN client after running the script. If
# an existing x3mRouting_client_rules file exists, a backup copy of the existing x3mRouting_client_rules file will be
# made by appending the timestamp to the existing x3mRouting_client_rules file.
#
#####################################################################################################
logger -st "($(basename "$0"))" $$ Starting Script Execution
# Uncomment the line below for debugging
#set -x
VERSION="1.0.5"
GITHUB_URL="https://github.com/Xentrk/x3mRouting"
CONFIG_FILE="/jffs/scripts/x3mRouting/x3mRouting_client_rules"
COLOR_WHITE='\033[0m'
COLOR_GREEN='\e[0;32m'
printf '\n'
printf '_______________________________________________________________\n'
printf '| |\n'
printf '| Welcome to the %bx3mRouting%b LAN Client Configuration Script |\n' "$COLOR_GREEN" "$COLOR_WHITE"
printf '| Version %b%s%b by Xentrk |\n' "$COLOR_GREEN" "$VERSION" "$COLOR_WHITE"
printf '| |\n'
printf '| Configuration instuctions available at |\n'
printf '| %b%s%b |\n' "$COLOR_GREEN" "$GITHUB_URL" "$COLOR_WHITE"
printf '| ____ _ _ |\n'
printf '| |__ | | | | | |\n'
printf '| __ __ _| |_ _ _ | |_ ___ | | __ ____ ____ _ _ _ |\n'
printf '| \ \/ / |_ | %b %b \ __|/ _ \| |/ / / _// \| %b %b \ |\n' "\`" "\`" "\`" "\`"
printf '| / / __| | | | | |_ | __/| < ( (_ | [] || | | | |\n'
printf '| /_/\_\|___ |_|_|_|\___|\___||_|\_\[] \___\\\____/|_|_|_| |\n'
printf '| |\n'
printf '|_____________________________________________________________|\n\n'
Parse_Hostnames() {
true >/tmp/hostnames.$$
OLDIFS=$IFS
IFS="<"
for ENTRY in $HOSTNAME_LIST; do
if [ "$ENTRY" = "" ]; then
continue
fi
MACID=$(echo "$ENTRY" | cut -d ">" -f 1)
HOSTNAME=$(echo "$ENTRY" | cut -d ">" -f 2)
echo "$MACID $HOSTNAME" >>/tmp/hostnames.$$
done
IFS=$OLDIFS
}
Clean_Up()
{
[ -f /tmp/static_mac.$$ ] && rm -rf /tmp/static_mac.$$
[ -f /tmp/static_ip.$$ ] && rm -rf /tmp/static_ip.$$
[ -f /tmp/staticlist.$$ ] && rm -rf /tmp/staticlist.$$
[ -f /tmp/hostnames.$$ ] && rm -rf /tmp/hostnames.$$
[ -f /tmp/MACIPHOSTNAMES.$$ ] && rm -rf /tmp/MACIPHOSTNAMES.$$
}
Save_Dnsmasq_Format() {
# Obtain MAC and IP address from dhcp_staticlist and exclude DNS field by filtering using the first three octets of the lan_ipaddr
if [ -s /jffs/nvram/dhcp_staticlist ]; then #HND Routers store dhcp_staticlist in a file
awk '{print $0}' /jffs/nvram/dhcp_staticlist | grep -oE "((([0-9a-fA-F]{2})[ :-]){5}[0-9a-fA-F]{2})|(([0-9a-fA-F]){6}[:-]([0-9a-fA-F]){6})|([0-9a-fA-F]{12})" >/tmp/static_mac.$$
awk '{print $0}' /jffs/nvram/dhcp_staticlist | grep -oE "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" | grep "$(nvram get lan_ipaddr | grep -Eo '([0-9]{1,3}\.[0-9]{1,3}(\.[0-9]{1,3}))')" >/tmp/static_ip.$$
else # non-HND Routers store dhcp_staticlist in nvram
nvram get dhcp_staticlist | grep -oE "((([0-9a-fA-F]{2})[ :-]){5}[0-9a-fA-F]{2})|(([0-9a-fA-F]){6}[:-]([0-9a-fA-F]){6})|([0-9a-fA-F]{12})" >/tmp/static_mac.$$
nvram get dhcp_staticlist | grep -oE "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" | grep "$(nvram get lan_ipaddr | grep -Eo '([0-9]{1,3}\.[0-9]{1,3}(\.[0-9]{1,3}))')" >/tmp/static_ip.$$
fi
if [ ! -s /tmp/static_mac.$$ ] || [ ! -s /tmp/static_ip.$$ ]; then
printf 'Aborting script. No DHCP Static Lease Reservations found.\n'
printf 'Please create DHCP Static Leases in the LAN->DHCP Server screen.\n'
Clean_Up
exit 1
fi
# output /tmp/static_mac.$$ and /tmp/static_ip.$$ to /tmp/staticlist.$$ in two columns side by side
#https://www.unix.com/shell-programming-and-scripting/161826-how-combine-2-files-into-1-file-2-columns.html
awk 'NR==FNR{a[i++]=$0};{b[x++]=$0;};{k=x-i};END{for(j=0;j<i;) print a[j++],b[k++]}' /tmp/static_mac.$$ /tmp/static_ip.$$ >/tmp/staticlist.$$
if [ -s /jffs/nvram/dhcp_hostnames ]; then #HND Routers store hostnames in a file
HOSTNAME_LIST=$(awk '{print $0}' /jffs/nvram/dhcp_hostnames | sed 's/>undefined//')
else
HOSTNAME_LIST=$(nvram get dhcp_hostnames | sed 's/>undefined//')
fi
# Have to parse by internal field separator since hostnames are not required
Parse_Hostnames
# Join the /tmp/hostnames.$$ and /tmp/staticlist.$$ files together to form one file containing MAC, IP, HOSTNAME
awk '
NR==FNR { k[$1]=$2; next }
{ print $0, k[$1] }
' /tmp/hostnames.$$ /tmp/staticlist.$$ >/tmp/MACIPHOSTNAMES.$$
}
# Begin
# Check to see if a prior x3mRouting_rules file exists. Make a backup if it does.
if [ -s "$CONFIG_FILE" ]; then
TIMESTAMP=$(date +"%Y-%m-%d-%H.%M.%S")
if ! mv "$CONFIG_FILE" "$CONFIG_FILE"."$TIMESTAMP"; then
printf '\nBackup of the prior %b%s%b file could not be made.\n' "$COLOR_GREEN" "$CONFIG_FILE" "$COLOR_WHITE"
else
printf '\nExisting %b%s%b file found.\n' "$COLOR_GREEN" "$CONFIG_FILE" "$COLOR_WHITE"
printf 'A backup of the existing file was made.\n'
fi
fi
Save_Dnsmasq_Format
# write a new x3mRouting_rules file
true >"$CONFIG_FILE"
# ignore mac addr and write IP and HOSTNAME for Static DHCP LAN Clients to $CONFIG_FILE
while read -r _ IP HOSTNAME; do
echo "$IP $HOSTNAME" >>$CONFIG_FILE
done </tmp/MACIPHOSTNAMES.$$
sort -ut '.' -k 1,1n -k 2,2n -k 3,3n -k 4,4n "$CONFIG_FILE" -o "$CONFIG_FILE"
Clean_Up
# Default all lan clients to OVPNC1 interface
sed -i -e 's/^/1 /' "$CONFIG_FILE"
# Add instructions at the top of the file
sed -i '1s/^/#########################################################\n/' "$CONFIG_FILE"
sed -i '1s/^/# 5 = OVPNC5 #\n/' "$CONFIG_FILE"
sed -i '1s/^/# 4 = OVPNC4 #\n/' "$CONFIG_FILE"
sed -i '1s/^/# 3 = OVPNC3 #\n/' "$CONFIG_FILE"
sed -i '1s/^/# 2 = OVPNC2 #\n/' "$CONFIG_FILE"
sed -i '1s/^/# 1 = OVPNC1 #\n/' "$CONFIG_FILE"
sed -i '1s/^/# 0 = WAN #\n/' "$CONFIG_FILE"
sed -i '1s/^/# the appropriate interface number in the first column #\n/' "$CONFIG_FILE"
sed -i '1s/^/# Assign the interface for each LAN client by entering #\n/' "$CONFIG_FILE"
sed -i '1s/^/#########################################################\n/' "$CONFIG_FILE"
printf '\nYou must now edit %b%s%b and\n' "$COLOR_GREEN" "$CONFIG_FILE" "$COLOR_WHITE"
printf 'assign the interface for each LAN client.\n'
printf 'When done, execute the %bx3mRouting_client_nvram.sh%b script and bounce the VPN Clients\n' "$COLOR_GREEN" "$COLOR_WHITE"
printf '\n'
logger -st "($(basename "$0"))" $$ Ending Script Execution