onsubnet 811 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env bash
  2. if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]] || [[ "$1" == "" ]] ; then
  3. printf "Usage:\n\tonsubnet [ --not ] partial-ip-address\n\n"
  4. printf "Example:\n\tonsubnet 10.10.\n\tonsubnet --not 192.168.0.\n\n"
  5. printf "Note:\n\tThe partial-ip-address must match starting at the first\n"
  6. printf "\tcharacter of the ip-address, therefore the first example\n"
  7. printf "\tabove will match 10.10.10.1 but not 110.10.10.1\n"
  8. exit 0
  9. fi
  10. on=0
  11. off=1
  12. if [[ "$1" == "--not" ]] ; then
  13. shift
  14. on=1
  15. off=0
  16. fi
  17. regexp="^$(sed 's/\./\\./g' <<<"$1")"
  18. if [[ "$(uname)" == "Darwin" ]] ; then
  19. ifconfig | grep -F 'inet ' | grep -Fv 127.0.0. | cut -d ' ' -f 2 | grep $1
  20. else
  21. hostname -I | tr -s " " "\012" | grep -Fv 127.0.0. | grep $1
  22. fi
  23. if [[ $? == 0 ]]; then
  24. exit $on
  25. else
  26. exit $off
  27. fi