blob: 038b1f7f1cfb9c95e53f89cdecdb8263e6888f7f [file] [log] [blame]
Denys Vlasenko6cf7f012009-11-06 04:04:19 +01001#!/bin/sh
2# convert:
3
4# dhcptype=5
5# serverid=172.16.42.102
6# lease=97200
7# interface=eth0
8# ip=172.16.42.177
9# subnet=255.255.255.0
10# mask=24
11# broadcast=172.16.22.255
12# router=172.16.42.98
13# dns=10.34.32.125 10.32.63.5 10.34.255.7 10.11.255.27
14# domain=lab.example.com example.com
15# ntpsrv=10.34.32.125 10.34.255.7
16
17# into:
18
19#let cfg=cfg+1
20#if[$cfg]=...; ip[$cfg]=...; ipmask[$cfg]=.../...; gw[$cfg]=...; net[$cfg]=... dns[$cfg]=...
21
Denys Vlasenko391dd922009-11-09 09:39:50 +010022exec >/dev/null
23#exec >"$0.out" # debug
24exec 2>&1
Denys Vlasenko6cf7f012009-11-06 04:04:19 +010025
26test "$interface" || exit 1
Denys Vlasenko391dd922009-11-09 09:39:50 +010027test "$ip" || exit 1
Denys Vlasenko6cf7f012009-11-06 04:04:19 +010028
Denys Vlasenko2feaba12019-02-05 17:48:24 +010029# some servers do not return subnet option.
30# guess it for standard private networks.
31if ! test "$mask"; then
32 if test "$ip" != "${ip#192.168.}"; then
33 mask=16
34 elif test "$ip" != "${ip#172.16.}"; then
35 mask=12
36 # repeat for each in 172.17. - 172.31. range?
37 elif test "$ip" != "${ip#10.}"; then
38 mask=8
39 fi
40fi
41
42# some servers do not return router option.
43# assume DHCP server is the router.
44if ! test "$router"; then
45 test "$serverid" && router="$serverid"
46fi
47
Denys Vlasenko6cf7f012009-11-06 04:04:19 +010048{
49echo "let cfg=cfg+1"
Denys Vlasenko3581c622010-01-25 13:39:24 +010050test "$interface" && echo "if[\$cfg]='$interface'"
Denys Vlasenko6cf7f012009-11-06 04:04:19 +010051test "$ip" && echo "ip[\$cfg]='$ip'"
52test "$ip" && test "$mask" \
53 && echo "ipmask[\$cfg]='$ip/$mask'"
54test "$router" && echo "gw[\$cfg]='$router'"
55test "$dns" && echo "dns[\$cfg]='$dns'"
56# TODO: I never saw a dhcp server which correctly announces
57# which subnet(s) is/are available thru advertised router
58# Assume 0/0
59echo "net[\$cfg]='0/0'"
Denys Vlasenko391dd922009-11-09 09:39:50 +010060} >"$1"