#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --host 127.0.0.1 --port 80,443 [--timeout 3]" } host="127.0.0.1" ports="" timeout="3" while [[ $# -gt 0 ]]; do case "$1" in --host) host="${2:-}"; shift 2 ;; --port) ports="${2:-}"; shift 2 ;; --timeout) timeout="${2:-}"; shift 2 ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done if [[ -z "$ports" ]]; then echo "Укажите порты через --port" >&2 exit 2 fi if ! command -v nc >/dev/null 2>&1; then echo "Не найден nc" >&2 exit 3 fi status=0 IFS=',' read -r -a port_list <<<"$ports" for port in "${port_list[@]}"; do port="${port// /}" if [[ -z "$port" ]]; then continue fi if [[ ! "$port" =~ ^[0-9]+$ ]]; then echo "Некорректный порт: $port" >&2 status=2 continue fi if nc -z -w "$timeout" "$host" "$port" >/dev/null 2>&1; then printf '%s:%s OPEN\n' "$host" "$port" else printf '%s:%s CLOSED\n' "$host" "$port" status=2 fi done if (( status == 0 )); then echo "OK" else echo "CRITICAL: есть закрытые порты" >&2 fi exit "$status"