#!/bin/bash
# identify-hosts.sh — identify all live hosts on a network via DNS/SNMP/SSH
# Usage: bash identify-hosts.sh <hosts-file-or-subnet>
#   bash identify-hosts.sh 192.168.0.0/22
#   bash identify-hosts.sh /tmp/live-hosts.txt
set -u
COMMUNITY="${SNMP_COMMUNITY:-kn3lmgmt}"

if [ -f "${1:-}" ]; then
  HOSTS=$(cat "$1")
elif [ -n "${1:-}" ]; then
  HOSTS=$(nmap -sn -n "$1" 2>/dev/null | grep 'Nmap scan report' | awk '{print $5}')
else
  echo "Usage: $0 <hosts-file-or-subnet>"
  exit 1
fi

printf "%-16s %-30s %-25s %-20s\n" "IP" "DNS_NAME" "SNMP_SYSDESCR" "SSH_BANNER"
printf "%-16s %-30s %-25s %-20s\n" "--" "-------" "-------------" "----------"

for ip in $HOSTS; do
  # DNS reverse
  rdns=$(dig +short -x "$ip" 2>/dev/null | head -1 | sed 's/\.$//')
  [ -z "$rdns" ] && rdns="(no-ptr)"

  # SNMP sysDescr (first 25 chars)
  snmp=$(timeout 3 snmpget -Oqv -v2c -c "$COMMUNITY" "$ip" 1.3.6.1.2.1.1.1.0 2>/dev/null | head -1 | tr -d '"' | cut -c1-25)
  [ -z "$snmp" ] && snmp="-"

  # SSH banner (quick)
  ssh_banner=$(timeout 3 bash -c "echo > /dev/tcp/$ip/22" 2>/dev/null && echo "ssh:open" || echo "-")

  printf "%-16s %-30s %-25s %-20s\n" "$ip" "$rdns" "$snmp" "$ssh_banner"
done
