Denys Vlasenko | 6cf7f01 | 2009-11-06 04:04:19 +0100 | [diff] [blame] | 1 | #!/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 Vlasenko | b9c2108 | 2022-05-12 11:07:12 +0200 | [diff] [blame] | 22 | #exec >/dev/null |
Denys Vlasenko | 391dd92 | 2009-11-09 09:39:50 +0100 | [diff] [blame] | 23 | #exec >"$0.out" # debug |
| 24 | exec 2>&1 |
Denys Vlasenko | 6cf7f01 | 2009-11-06 04:04:19 +0100 | [diff] [blame] | 25 | |
| 26 | test "$interface" || exit 1 |
Denys Vlasenko | 391dd92 | 2009-11-09 09:39:50 +0100 | [diff] [blame] | 27 | test "$ip" || exit 1 |
Denys Vlasenko | 6cf7f01 | 2009-11-06 04:04:19 +0100 | [diff] [blame] | 28 | |
Denys Vlasenko | 2feaba1 | 2019-02-05 17:48:24 +0100 | [diff] [blame] | 29 | # some servers do not return subnet option. |
| 30 | # guess it for standard private networks. |
| 31 | if ! test "$mask"; then |
Denys Vlasenko | 5a387e2 | 2019-02-08 10:37:05 +0100 | [diff] [blame] | 32 | case "$ip" in |
| 33 | 10.*) |
Denys Vlasenko | b9c2108 | 2022-05-12 11:07:12 +0200 | [diff] [blame] | 34 | echo "mask assumed 8 for ip=$ip" |
Denys Vlasenko | 5a387e2 | 2019-02-08 10:37:05 +0100 | [diff] [blame] | 35 | mask=8;; |
Denys Vlasenko | 5a387e2 | 2019-02-08 10:37:05 +0100 | [diff] [blame] | 36 | #172.16-31.x.x |
| 37 | 172.1[6789].*) |
Denys Vlasenko | b9c2108 | 2022-05-12 11:07:12 +0200 | [diff] [blame] | 38 | echo "mask assumed 12 for ip=$ip" |
Denys Vlasenko | 5a387e2 | 2019-02-08 10:37:05 +0100 | [diff] [blame] | 39 | mask=12;; |
| 40 | 172.2[0123456789].*) |
Denys Vlasenko | b9c2108 | 2022-05-12 11:07:12 +0200 | [diff] [blame] | 41 | echo "mask assumed 12 for ip=$ip" |
Denys Vlasenko | 5a387e2 | 2019-02-08 10:37:05 +0100 | [diff] [blame] | 42 | mask=12;; |
| 43 | 172.3[01].*) |
Denys Vlasenko | b9c2108 | 2022-05-12 11:07:12 +0200 | [diff] [blame] | 44 | echo "mask assumed 12 for ip=$ip" |
Denys Vlasenko | 5a387e2 | 2019-02-08 10:37:05 +0100 | [diff] [blame] | 45 | mask=12;; |
Denys Vlasenko | b9c2108 | 2022-05-12 11:07:12 +0200 | [diff] [blame] | 46 | 192.168.*) |
| 47 | echo "mask assumed 16 for ip=$ip" |
| 48 | mask=16;; |
Denys Vlasenko | 5a387e2 | 2019-02-08 10:37:05 +0100 | [diff] [blame] | 49 | esac |
Denys Vlasenko | 2feaba1 | 2019-02-05 17:48:24 +0100 | [diff] [blame] | 50 | fi |
| 51 | |
| 52 | # some servers do not return router option. |
| 53 | # assume DHCP server is the router. |
| 54 | if ! test "$router"; then |
Denys Vlasenko | b9c2108 | 2022-05-12 11:07:12 +0200 | [diff] [blame] | 55 | if test "$serverid"; then |
| 56 | router="$serverid" |
| 57 | echo "No 'router' from the server, assuming 'serverid' is the router: $serverid" |
| 58 | fi |
Denys Vlasenko | 2feaba1 | 2019-02-05 17:48:24 +0100 | [diff] [blame] | 59 | fi |
| 60 | |
Denys Vlasenko | 6cf7f01 | 2009-11-06 04:04:19 +0100 | [diff] [blame] | 61 | { |
| 62 | echo "let cfg=cfg+1" |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 63 | test "$interface" && echo "if[\$cfg]='$interface'" |
Denys Vlasenko | 6cf7f01 | 2009-11-06 04:04:19 +0100 | [diff] [blame] | 64 | test "$ip" && echo "ip[\$cfg]='$ip'" |
| 65 | test "$ip" && test "$mask" \ |
| 66 | && echo "ipmask[\$cfg]='$ip/$mask'" |
| 67 | test "$router" && echo "gw[\$cfg]='$router'" |
| 68 | test "$dns" && echo "dns[\$cfg]='$dns'" |
| 69 | # TODO: I never saw a dhcp server which correctly announces |
| 70 | # which subnet(s) is/are available thru advertised router |
| 71 | # Assume 0/0 |
| 72 | echo "net[\$cfg]='0/0'" |
Denys Vlasenko | 391dd92 | 2009-11-09 09:39:50 +0100 | [diff] [blame] | 73 | } >"$1" |