summaryrefslogtreecommitdiff
path: root/examples/var_service/dhcp_if/convert2ipconf
blob: 31e3c7fde159e2bf1e41c83bf0fbbc5c36ee3eb9 (plain)
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
#!/bin/sh
# convert:

# dhcptype=5
# serverid=172.16.42.102
# lease=97200
# interface=eth0
# ip=172.16.42.177
# subnet=255.255.255.0
# mask=24
# broadcast=172.16.22.255
# router=172.16.42.98
# dns=10.34.32.125 10.32.63.5 10.34.255.7 10.11.255.27
# domain=lab.example.com example.com
# ntpsrv=10.34.32.125 10.34.255.7

# into:

#let cfg=cfg+1
#if[$cfg]=...; ip[$cfg]=...; ipmask[$cfg]=.../...; gw[$cfg]=...; net[$cfg]=... dns[$cfg]=...

exec >/dev/null
#exec >"$0.out"  # debug
exec 2>&1

test "$interface" || exit 1
test "$ip" || exit 1

# some servers do not return subnet option.
# guess it for standard private networks.
if ! test "$mask"; then
	case "$ip" in
	10.*)
		mask=8;;
	192.168.*)
		mask=16;;
	#172.16-31.x.x
	172.1[6789].*)
		mask=12;;
	172.2[0123456789].*)
		mask=12;;
	172.3[01].*)
		mask=12;;
	esac
fi

# some servers do not return router option.
# assume DHCP server is the router.
if ! test "$router"; then
	test "$serverid" && router="$serverid"
fi

{
echo "let cfg=cfg+1"
test "$interface"	&& echo "if[\$cfg]='$interface'"
test "$ip"		&& echo "ip[\$cfg]='$ip'"
test "$ip" && test "$mask" \
			&& echo "ipmask[\$cfg]='$ip/$mask'"
test "$router"		&& echo "gw[\$cfg]='$router'"
test "$dns"		&& echo "dns[\$cfg]='$dns'"
# TODO: I never saw a dhcp server which correctly announces
# which subnet(s) is/are available thru advertised router
# Assume 0/0
echo "net[\$cfg]='0/0'"
} >"$1"