3
0

convert2ipconf 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. echo "mask assumed 8 for ip=$ip"
  29. mask=8;;
  30. #172.16-31.x.x
  31. 172.1[6789].*)
  32. echo "mask assumed 12 for ip=$ip"
  33. mask=12;;
  34. 172.2[0123456789].*)
  35. echo "mask assumed 12 for ip=$ip"
  36. mask=12;;
  37. 172.3[01].*)
  38. echo "mask assumed 12 for ip=$ip"
  39. mask=12;;
  40. 192.168.*)
  41. echo "mask assumed 16 for ip=$ip"
  42. mask=16;;
  43. esac
  44. fi
  45. # some servers do not return router option.
  46. # assume DHCP server is the router.
  47. if ! test "$router"; then
  48. if test "$serverid"; then
  49. router="$serverid"
  50. echo "No 'router' from the server, assuming 'serverid' is the router: $serverid"
  51. fi
  52. fi
  53. {
  54. echo "let cfg=cfg+1"
  55. test "$interface" && echo "if[\$cfg]='$interface'"
  56. test "$ip" && echo "ip[\$cfg]='$ip'"
  57. test "$ip" && test "$mask" \
  58. && echo "ipmask[\$cfg]='$ip/$mask'"
  59. test "$router" && echo "gw[\$cfg]='$router'"
  60. test "$dns" && echo "dns[\$cfg]='$dns'"
  61. # TODO: I never saw a dhcp server which correctly announces
  62. # which subnet(s) is/are available thru advertised router
  63. # Assume 0/0
  64. echo "net[\$cfg]='0/0'"
  65. } >"$1"