executable_scan 788 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/env bash
  2. # Scan LAN and report in human format.
  3. set -eu
  4. set -o pipefail
  5. do_scan()
  6. {
  7. out="$(sudo nmap -oX - -sN -p "${SCAN_PORT:-22}" "${SCAN_NETWORK:-192.168.100.0/24}" | xq '.nmaprun.host | map(select(.ports.port.state."@state" != "closed") | .address)')"
  8. echo "$out" | jq -r 'map(select(type == "array"))[] | .[] | ."@addr"' | paste -d '\t' - -
  9. echo "$out" | jq -r 'map(select(type == "object"))[] | ."@addr"'
  10. }
  11. case "$1" in
  12. lan)
  13. do_scan
  14. ;;
  15. lab)
  16. for host in $(do_scan | awk '{ print $1 }')
  17. do
  18. if [[ "$(timeout 3 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -q root@"$host" uname -a)" == *Debian* ]]
  19. then
  20. echo "$host"
  21. fi
  22. done
  23. ;;
  24. esac