lib/portscan.sh

587 B · 27 lines · bash

1#!/usr/bin/env bash
2
3brt_portscan() {
4 local host="${1:-localhost}"
5 local port_spec="${2:-}"
6
7 if [[ -z "$host" ]]; then
8 brt_error "Usage: brt -portscan <host> [ports|start-end]"
9 exit 1
10 fi
11
12 brt_info "Scanning ${host}..."
13 local open=0 total=0
14
15 while IFS= read -r port; do
16 [[ -z "$port" ]] && continue
17 ((total++)) || true
18 if (echo >/dev/tcp/"$host"/"$port") 2>/dev/null; then
19 echo -e "\033[32mOPEN\033[0m ${host}:${port}"
20 ((open++)) || true
21 fi
22 done < <(brt_parse_ports "$port_spec")
23
24 echo
25 brt_ok "Done: ${open}/${total} ports open"
26}
27