dhclient 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/bin/sh
  2. # Begin services/dhclient
  3. # Origianlly based upon lfs-bootscripts-1.12 $NETWORK_DEVICES/if{down,up}
  4. # Rewritten by Nathan Coulson <nathan@linuxfromscratch.org>
  5. # Adapted for dhclient by DJ Lucas <dj@linuxfromscratch.org>
  6. # Update for LFS 7.0 by Ken Moffat <ken@linuxfromscratch.org>
  7. # Call with: IFCONFIG=<filename> /lib/services/dhclient <IFACE> <up | down>
  8. #$LastChangedBy: bdubbs $
  9. #$Date: 2014-08-27 13:01:33 -0500 (Wed, 27 Aug 2014) $
  10. . /lib/lsb/init-functions
  11. . $IFCONFIG
  12. PIDFILE=/run/dhclient-$1.pid
  13. LFILE=/var/lib/dhclient/dhclient-$1.leases
  14. getipstats()
  15. {
  16. # Print the last 16 lines of dhclient.leases
  17. sed -e :a -e '$q;N;17,$D;ba' ${LFILE}
  18. }
  19. # Make compatible with older versions of init-functions
  20. unset is_true
  21. is_true()
  22. {
  23. [ "$1" = "1" ] || [ "$1" = "yes" ] || [ "$1" = "true" ] ||
  24. [ "$1" = "y" ] || [ "$1" = "t" ]
  25. }
  26. case "$2" in
  27. up)
  28. if [ -e ${PIDFILE} ]; then
  29. ps $(cat ${PIDFILE}) | grep dhclient >/dev/null
  30. if [ "$?" = "0" ]; then
  31. log_warning_msg "\n dhclient appears to be running on $1"
  32. exit 0
  33. else
  34. rm ${PIDFILE}
  35. fi
  36. fi
  37. log_info_msg "\n Starting dhclient on the $1 interface..."
  38. /sbin/dhclient -lf ${LFILE} -pf ${PIDFILE} $DHCP_START $1
  39. if [ "$?" != "0" ]; then
  40. log_failure_msg2
  41. exit 1
  42. fi
  43. # Print the assigned settings if requested
  44. if is_true "$PRINTIP" -o is_true "$PRINTALL"; then
  45. # Get info from dhclient.leases file
  46. IPADDR=`getipstats | grep "fixed-address" | \
  47. sed 's/ fixed-address //' | \
  48. sed 's/\;//'`
  49. NETMASK=`getipstats | grep "subnet-mask" | \
  50. sed 's/ option subnet-mask //' | \
  51. sed 's/\;//'`
  52. GATEWAY=`getipstats | grep "routers" | \
  53. sed 's/ option routers //' | \
  54. sed 's/\;//'`
  55. DNS=`getipstats | grep "domain-name-servers" | \
  56. sed 's/ option domain-name-servers //' | \
  57. sed 's/\;//' | sed 's/,/ and /'`
  58. if [ "$PRINTALL" = "yes" ]; then
  59. # This is messy, the messages are on one very long
  60. # line on the screen and in the log
  61. log_info_msg " DHCP Assigned Settings for $1:"
  62. log_info_msg " IP Address: $IPADDR"
  63. log_info_msg " Subnet Mask: $NETMASK"
  64. log_info_msg " Default Gateway: $GATEWAY"
  65. log_info_msg " DNS Server: $DNS"
  66. else
  67. log_info_msg " IP Addresss:""$IPADDR"
  68. fi
  69. fi
  70. log_success_msg2
  71. ;;
  72. down)
  73. if [ ! -e ${PIDFILE} ]; then
  74. log_warning_msg "\n dhclient doesn't appear to be running on $1"
  75. exit 0
  76. fi
  77. log_info_msg "\n Stopping dhclient on the $1 interface..."
  78. /sbin/dhclient -r -lf ${LFILE} -pf ${PIDFILE} $DHCP_STOP $1
  79. if [ -e ${PIDFILE} ]; then
  80. ps $(cat ${PIDFILE}) | grep dhclient >/dev/null
  81. if [ "$?" != "0" ]; then
  82. rm -f ${PIDFILE}
  83. fi
  84. fi
  85. evaluate_retval
  86. ;;
  87. *)
  88. echo "Usage: $0 [interface] {up|down}"
  89. exit 1
  90. ;;
  91. esac
  92. # End services/dhclient