1
0

ifdown 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/bin/sh
  2. ########################################################################
  3. # Begin /sbin/ifdown
  4. #
  5. # Description : Interface Down
  6. #
  7. # Authors : DJ Lucas - dj@linuxfromscratch.org
  8. #
  9. # Version : 00.02
  10. #
  11. ########################################################################
  12. . /lib/lsb/init-functions
  13. function get_args()
  14. {
  15. if test -z "${1}" ; then
  16. showhelp
  17. exit 1
  18. fi
  19. while test -n "${1}" ; do
  20. case "${1}" in
  21. -c | --configfile)
  22. check_arg $1 $2
  23. CONFIGFILE="${2}"
  24. shift 2
  25. ;;
  26. -f | --force)
  27. FORCE="1"
  28. shift 1
  29. ;;
  30. eth* | iw* | wlan*)
  31. INTERFACE="${1}"
  32. shift 1
  33. ;;
  34. -h | --help)
  35. showhelp
  36. exit 0
  37. ;;
  38. *)
  39. showhelp
  40. echo "ERROR: '${1}' unknown argument"
  41. echo ""
  42. exit 2
  43. ;;
  44. esac
  45. done
  46. }
  47. function check_arg()
  48. {
  49. echo "${2}" | grep -v "^-" > /dev/null
  50. if [ -z "${?}" -o ! -n "${2}" ]; then
  51. echo "Error: ${1} requires a valid argument."
  52. exit 2
  53. fi
  54. }
  55. function showhelp()
  56. {
  57. echo ""
  58. echo "`/usr/bin/basename ${0}` brings down a valid network interface."
  59. echo ""
  60. echo "Options:"
  61. echo " -c --configfile The path to an interface configuration file"
  62. echo " If no configuration file is given, all files"
  63. echo " listed in /etc/network/ifconfig.<int> will"
  64. echo " be processed, regarless of the value of ONBOOT"
  65. echo " -f --force Flush all IPs and force the interface down."
  66. echo " -h --help Show this help message and exit."
  67. echo ""
  68. echo "Examples:"
  69. echo " `/usr/bin/basename ${0}` eth0 -c /run/network/ifconfig.eth0/ipv4"
  70. echo " `/usr/bin/basename ${0}` eth0 --force -c /run/network/ifconfig.eth0/ipv4"
  71. echo " `/usr/bin/basename ${0}` eth0 --force"
  72. echo " `/usr/bin/basename ${0}` eth0"
  73. echo ""
  74. echo ""
  75. }
  76. # Intialize empty variables so that the shell does not polute the script
  77. CONFIGFILE=""
  78. CONFIGDIR=""
  79. INTERFACE=""
  80. FORCE=""
  81. failed=0
  82. # Process command line arguments
  83. get_args ${@}
  84. # Handle common errors - No need to account for bootscripts, this should not
  85. # happen during boot or shutdown.
  86. if [ "${CONFIGFILE}x" != "x" -a ! -f "${CONFIGFILE}" ]; then
  87. echo "ERROR: ${CONFIGFILE} is not a valid network configuration file."
  88. echo ""
  89. exit 2
  90. fi
  91. if [ "${INTERFACE}x" == "x" ]; then
  92. echo "ERROR: No interface was given"
  93. echo ""
  94. exit 2
  95. else
  96. if ! grep "${INTERFACE}" /proc/net/dev 2>&1 > /dev/null; then
  97. echo "ERROR: ${INTERFACE} is not a valid network interface."
  98. echo ""
  99. exit 2
  100. fi
  101. fi
  102. # If a configuration file is present, use it
  103. if [ "${CONFIGFILE}x" != "x" ]; then
  104. . "${CONFIGFILE}"
  105. if [ -x "/lib/network-services/${SERVICE}" ]; then
  106. # do the work
  107. if IFCONFIG=${CONFIGFILE} \
  108. /lib/network-services/${SERVICE} ${INTERFACE} down; then
  109. rm "${CONFIGFILE}"
  110. fi
  111. else
  112. echo "ERROR: Service '${SERVICE}' is not a valid service."
  113. echo ""
  114. exit 2
  115. fi
  116. # No interface configuration file was given
  117. else
  118. # Process all running interface configuration files
  119. CONFIGDIR="/run/network/ifconfig.${INTERFACE}"
  120. if [ -d "${CONFIGDIR}" ]; then
  121. FILES=`ls "${CONFIGDIR}"`
  122. for CONFIGFILE in ${FILES}
  123. do
  124. (
  125. . "${CONFIGDIR}/${CONFIGFILE}"
  126. # No error checking necessary if they are in /run
  127. if IFCONFIG="${CONFIGDIR}/${CONFIGFILE}" \
  128. /lib/network-services/${SERVICE} ${INTERFACE} down; then
  129. rm "${CONFIGDIR}/${CONFIGFILE}"
  130. fi
  131. )
  132. done
  133. # all running config files processes, set the link down
  134. message="Setting interface ${INTERFACE} down..."
  135. /sbin/ip link set "${INTERFACE}" down
  136. evaluate_retval standard
  137. else
  138. if [ "${FORCE}" != "1" ]; then
  139. echo "ERROR: No configuration files found for ${INTERFACE}."
  140. echo ""
  141. exit 2
  142. fi
  143. fi
  144. fi
  145. if [ "${FORCE}" == "1" ]; then
  146. /sbin/ip addr flush dev "${INTERFACE}" 2>&1 > /dev/null || failed=1
  147. if [ "${failed}" == "1" ]; then
  148. log_failure_msg "Flushing IP addresses from interface ${INTERFACE}..."
  149. echo ""
  150. exit 1
  151. else
  152. log_success_msg "Flushing IP addresses from interface ${INTERFACE}..."
  153. fi
  154. /sbin/ip link set dev "${INTERFACE}" down 2>&1 > /dev/null || failed=1
  155. if [ "${failed}" == "1" ]; then
  156. log_failure_msg "Setting link down for interface ${INTERFACE}..."
  157. echo ""
  158. exit 1
  159. else
  160. log_success_msg "Setting link down for interface ${INTERFACE}..."
  161. fi
  162. fi
  163. exit "${failed}"