with_addr.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # add private ipv4 and ipv6 addresses to loopback
  5. readonly V6_INNER='100::a/128'
  6. readonly V4_INNER='192.168.0.1/32'
  7. if getopts ":s" opt; then
  8. readonly SIT_DEV_NAME='sixtofourtest0'
  9. readonly V6_SIT='2::/64'
  10. readonly V4_SIT='172.17.0.1/32'
  11. shift
  12. fi
  13. fail() {
  14. echo "error: $*" 1>&2
  15. exit 1
  16. }
  17. setup() {
  18. ip -6 addr add "${V6_INNER}" dev lo || fail 'failed to setup v6 address'
  19. ip -4 addr add "${V4_INNER}" dev lo || fail 'failed to setup v4 address'
  20. if [[ -n "${V6_SIT}" ]]; then
  21. ip link add "${SIT_DEV_NAME}" type sit remote any local any \
  22. || fail 'failed to add sit'
  23. ip link set dev "${SIT_DEV_NAME}" up \
  24. || fail 'failed to bring sit device up'
  25. ip -6 addr add "${V6_SIT}" dev "${SIT_DEV_NAME}" \
  26. || fail 'failed to setup v6 SIT address'
  27. ip -4 addr add "${V4_SIT}" dev "${SIT_DEV_NAME}" \
  28. || fail 'failed to setup v4 SIT address'
  29. fi
  30. sleep 2 # avoid race causing bind to fail
  31. }
  32. cleanup() {
  33. if [[ -n "${V6_SIT}" ]]; then
  34. ip -4 addr del "${V4_SIT}" dev "${SIT_DEV_NAME}"
  35. ip -6 addr del "${V6_SIT}" dev "${SIT_DEV_NAME}"
  36. ip link del "${SIT_DEV_NAME}"
  37. fi
  38. ip -4 addr del "${V4_INNER}" dev lo
  39. ip -6 addr del "${V6_INNER}" dev lo
  40. }
  41. trap cleanup EXIT
  42. setup
  43. "$@"
  44. exit "$?"