| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | brt_listen() { |
| 4 | local port="${1:-}" |
| 5 | |
| 6 | if [[ -n "$port" ]]; then |
| 7 | brt_info "Checking port ${port}..." |
| 8 | if command -v lsof &>/dev/null; then |
| 9 | lsof -i ":${port}" -sTCP:LISTEN 2>/dev/null || brt_warn "Port ${port} is not in use" |
| 10 | elif (echo >/dev/tcp/127.0.0.1/"$port") 2>/dev/null; then |
| 11 | brt_ok "Port ${port} is open" |
| 12 | else |
| 13 | brt_warn "Port ${port} is closed" |
| 14 | fi |
| 15 | return |
| 16 | fi |
| 17 | |
| 18 | brt_info "Listening ports:" |
| 19 | if command -v lsof &>/dev/null; then |
| 20 | lsof -iTCP -sTCP:LISTEN -P -n 2>/dev/null | awk 'NR==1 || /LISTEN/ {print}' |
| 21 | elif command -v netstat &>/dev/null; then |
| 22 | netstat -an | grep LISTEN |
| 23 | else |
| 24 | brt_error "lsof or netstat required" |
| 25 | exit 1 |
| 26 | fi |
| 27 | } |
| 28 | |