convert2ipconf 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/bin/sh
  2. # convert:
  3. # dhcptype=5
  4. # serverid=172.16.42.102
  5. # lease=97200
  6. # interface=eth0
  7. # ip=172.16.42.177
  8. # subnet=255.255.255.0
  9. # mask=24
  10. # broadcast=172.16.22.255
  11. # router=172.16.42.98
  12. # dns=10.34.32.125 10.32.63.5 10.34.255.7 10.11.255.27
  13. # domain=lab.example.com example.com
  14. # ntpsrv=10.34.32.125 10.34.255.7
  15. # into:
  16. #let cfg=cfg+1
  17. #if[$cfg]=...; ip[$cfg]=...; ipmask[$cfg]=.../...; gw[$cfg]=...; net[$cfg]=... dns[$cfg]=...
  18. exec >/dev/null
  19. #exec >"$0.out" # debug
  20. exec 2>&1
  21. test "$interface" || exit 1
  22. test "$ip" || exit 1
  23. # some servers do not return subnet option.
  24. # guess it for standard private networks.
  25. if ! test "$mask"; then
  26. case "$ip" in
  27. 10.*)
  28. mask=8;;
  29. 192.168.*)
  30. mask=16;;
  31. #172.16-31.x.x
  32. 172.1[6789].*)
  33. mask=12;;
  34. 172.2[0123456789].*)
  35. mask=12;;
  36. 172.3[01].*)
  37. mask=12;;
  38. esac
  39. fi
  40. # some servers do not return router option.
  41. # assume DHCP server is the router.
  42. if ! test "$router"; then
  43. test "$serverid" && router="$serverid"
  44. fi
  45. {
  46. echo "let cfg=cfg+1"
  47. test "$interface" && echo "if[\$cfg]='$interface'"
  48. test "$ip" && echo "ip[\$cfg]='$ip'"
  49. test "$ip" && test "$mask" \
  50. && echo "ipmask[\$cfg]='$ip/$mask'"
  51. test "$router" && echo "gw[\$cfg]='$router'"
  52. test "$dns" && echo "dns[\$cfg]='$dns'"
  53. # TODO: I never saw a dhcp server which correctly announces
  54. # which subnet(s) is/are available thru advertised router
  55. # Assume 0/0
  56. echo "net[\$cfg]='0/0'"
  57. } >"$1"