aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/open-ports.sh
blob: 9bac83c23a894d70eeadf5c8c6cc16f3c532a8b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env bash

# Copyright (c) 2023 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "audit-scripts" project.
# For details, see https://github.com/egor-tensin/audit-scripts.
# Distributed under the MIT License.

set -o errexit -o nounset -o pipefail
shopt -s inherit_errexit lastpipe

script_name="$( basename -- "${BASH_SOURCE[0]}" )"
readonly script_name

check_tool() {
    local tool
    for tool; do
        if ! command -v "$tool" > /dev/null; then
            echo "$script_name: $tool is missing" >&2
            exit 1
        fi
    done
}

print_usage() {
    echo "usage: $script_name [-h] [-6] [-p PORT_RANGES] [-t TOP_N_PORTS] HOST"
}

exit_with_usage_error() {
    local msg
    for msg; do
        echo "usage error: $msg" >&2
    done
    print_usage >&2
    exit 1
}

host=
declare -a nmap_args=()

parse_args() {
    local ports=
    local top_ports=

    local opt
    while getopts ':h6p:t:' opt; do
        case "$opt" in
            h)
                print_usage
                exit 0
                ;;
            6)
                nmap_args+=('-6')
                ;;
            p)
                ports="$OPTARG"
                ;;
            t)
                top_ports="$OPTARG"
                ;;
            :)
                exit_with_usage_error "option -$OPTARG requires an argument"
                ;;
            *)
                exit_with_usage_error "option -$OPTARG is invalid"
                ;;
        esac
    done
    shift "$((OPTIND - 1))"

    if [ "$#" -ne 1 ]; then
        exit_with_usage_error
    fi

    host="$1"

    if [ -n "$ports" ] && [ -n "$top_ports" ]; then
        exit_with_usage_error "you can't use both -p and -t options"
    fi
    if [ -z "$ports" ] && [ -z "$top_ports" ]; then
        ports='-'
    fi

    if [ -n "$ports" ]; then
        nmap_args+=('-p' "$ports")
    fi
    if [ -n "$top_ports" ]; then
        nmap_args+=('--top-ports' "$top_ports")
    fi
}

main() {
    check_tool nmap xmlstarlet
    parse_args "$@"

    nmap -Pn ${nmap_args[@]+"${nmap_args[@]}"} -oX - -- "$host" | xmlstarlet sel -t -v '//port[state/@state="open"]/@portid' -nl
}

main "$@"