-
Notifications
You must be signed in to change notification settings - Fork 0
/
convectix_netcfg.subr
258 lines (195 loc) · 4.88 KB
/
convectix_netcfg.subr
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/sh
STORE_FILE="/tmp/networks_store.txt"
CBSD_IP="/tmp/cbsdip.txt"
save_settings()
{
local out
[ -z "${out}" ] && out="/tmp/networks.${MYNIC}.txt"
eval IS_DHCP=\$dhcp_${MYNIC}
eval ip4=\$ip4_${MYNIC}
eval ip6=\$ip6_${MYNIC}
eval mask4=\$mask4_${MYNIC}
eval mask6=\$mask6_${MYNIC}
if [ "${IS_DHCP}" = "DHCP" ]; then
MYDHCP="YES"
else
MYDHCP="NO"
fi
cat >${out} <<EOF
nic: ${MYNIC}
defnic4: ${defnic4}
defnic6: ${defnic6}
ip4: ${ip4}
gw4: ${gw4}
ip6: ${ip6}
gw6: ${gw6}
mask4: ${mask4}
mask6: ${mask6}
dhcpd: ${MYDHCP}
EOF
}
# $1- nic
remove_dhcp_lease() {
leasefile="/var/db/dhclient.leases.${1}"
[ -f "${leasefile}" ] && rm -f ${leasefile}
}
dhcp_client() {
local i ALLNIC="$@" leasefile dhcp_cfg
[ -z "${ALLNIC}" ] && return 0
#renew only
if [ "${DHCP_TRY_MODE}" = "1" ]; then
dhcp_cfg="${_MYDIR}/dhclient-renew.conf"
sed -i '' s:CBSD_WORKDIR:${workdir}:g ${dhcp_cfg}
else
# apply
dhcp_cfg="${_MYDIR}/dhclient.conf"
fi
for i in ${ALLNIC}; do
${ECHO} " ${BOLD}*${N0_COLOR} ${N1_COLOR}Trying DHCP for : ${N2_COLOR}${i}${N0_COLOR}"
leasefile="/var/db/dhclient.leases.${i}"
[ -f "${leasefile}" ] && rm -f ${leasefile}
timeout 15 dhclient -c ${dhcp_cfg} ${i} > /dev/null 2>&1
killall -9 dhclient > /dev/null 2>&1
done
}
get_nic_ip() {
local _nic="${1}" ip4 ip6
[ -z "${_nic}" ] && return 0
ip4=$(ifconfig ${_nic} 2>/dev/null| ${AWK_CMD} '/inet [0-9]+/ { print $2}' | /usr/bin/head -n 1)
ip6=$(ifconfig ${_nic} 2>/dev/null| ${AWK_CMD} '/inet6 / { print $2}' | /usr/bin/head -n 1| grep -v ${_nic}\$)
if [ "${ip4}" != "0.0.0.0" ]; then
eval $( printf "ip4_${_nic}=\"${ip4}\"" )
fi
if [ -n "${ip6}" ]; then
eval $( printf "ip6_${_nic}=\"${ip6}\"" )
fi
}
get_nic_dhcp() {
local _nic="${1}"
[ -z "${_nic}" ] && return 0
# dhcp detect
leasefile="/var/db/dhclient.leases.${_nic}"
[ ! -f "${leasefile}" ] && return 0
dhcp_flags=$(stat -f %z ${leasefile} 2>&1)
if [ -n "${dhcp_flags}" ]; then
if [ ${dhcp_flags} -gt 0 ]; then
eval $( printf "dhcp_${_nic}='DHCP'" )
fi
fi
}
get_nic_mask() {
local _nic="${1}"
[ -z "${_nic}" ] && return 0
#hardcoded
mask4="255.255.255.0"
mask6=""
if [ -n "${mask4}" ]; then
eval $( printf "mask4_${_nic}=\"${mask4}\"" )
fi
if [ -n "${mask6}" ]; then
eval $( printf "mask6_${_nic}=\"${mask6}\"" )
fi
}
get_sys_route() {
local gateway interface
gw6=""
gw4=""
defnic4=""
defnic6=""
eval $( route -n get 0.0.0.0 2>/dev/null| ${AWK_CMD} '/gateway:|interface/{print $1$2}'|tr ":" "=" )
gw4="$gateway"
defnic4="$interface"
unset gateway
unset interface
eval $( route -n get -inet6 :: 2>/dev/null| ${AWK_CMD} '/gateway:|interface/{print $1$2}'|tr ":" "=" )
gw6="${gateway}"
defnic6="${interface}"
}
nic_test() {
local nic_num=0 ALLNIC NO_CHOOSED=1 ip4 ip6 is_dhcp
nic_num=$( ${miscdir}/nics-list -s lo|wc -l )
if [ ${nic_num} -eq 0 ]; then
${ECHO} "${N1_COLOR} No network card detected. Exit${N0_COLOR}"
exit 0
fi
ALLNIC=$( ${miscdir}/nics-list -s lo )
[ -n "${firststart}" ] && dhcp_client ${ALLNIC}
if [ ${nic_num} -eq 1 ]; then
MYNIC=${ALLNIC}
collect_net_data
save_settings
return 0
fi
${ECHO} "${N1_COLOR}You have multiple active nics. Please choose default interface${N0_COLOR}"
while [ ${NO_CHOOSED} -eq 1 ]; do
nic_num=0
for MYNIC in ${ALLNIC}; do
eval $( printf "selectnic${nic_num}=\"${MYNIC}\"" )
collect_net_data
eval is_dhcp=\$dhcp_${MYNIC}
eval ip4=\$ip4_${MYNIC}
eval ip6=\$ip6_${MYNIC}
printf "${nic_num}) ${MYNIC} ${is_dhcp} ${ip4} ${ip6}\n"
nic_num=$(( nic_num + 1 ))
save_settings
done
read getnic
[ ${getnic} -ge 0 -a ${getnic} -le ${nic_num} ] && NO_CHOOSED=0
done
eval MYNIC=\$selectnic${getnic}
eval IS_DHCP=\$dhcp_${MYNIC}
eval ip=\$ip4_${MYNIC}
${ECHO} "${N2_COLOR}$MYNIC ${N1_COLOR}Selected.${N0_COLOR}"
sleep 2
}
collect_net_data() {
get_sys_route
get_nic_ip ${MYNIC}
get_nic_mask ${MYNIC}
get_nic_dhcp ${MYNIC}
}
validate_nic() {
}
validate_ip4() {
}
validate_ip6() {
}
validate_gw4() {
}
validate_gw6() {
}
validate_mask4() {
}
validate_mask6() {
}
set_ip4() {
local _myval
if [ "${1}" = "DHCP" ]; then
_myval="DHCP"
else
_myval="inet ${ip4} netmask ${mask4}"
echo "${ip4}" > ${CBSD_IP}
fi
# echo "ifconfig_${nic}=\"inet ${ip4} netmask ${mask4}\""
# printf "Change in rc.conf: "
sysrc -q "ifconfig_${nic}=${_myval}" >/dev/null 2>&1
# daemon service netif restart
# daemon service routing restart
}
set_ip6() {
local _myval
if [ "${1}" = "DHCP" ]; then
_myval="DHCP"
else
_myval="inet6 ${ip6} netmask ${mask6}"
fi
printf "Change in rc.conf: "
echo sysrc -q "ipv6_activate_all_interfaces=\"YES\""
echo sysrc -q "ifconfig_${nic}_ipv6=\"${_myval}\""
}
set_gw4() {
sysrc -q "defaultrouter=${gw4}" >/dev/null 2>&1
}
set_gw6() {
echo sysrc -q "ipv6_defaultrouter=\"${gw6}\""
}