changelog shortlog graph tags branches changeset file revisions annotate raw help

Mercurial > infra > home / .stash/scripts/port-scan.sh

revision 48: 953c5127546a
parent 6: 45f3b1bc3cd7
     1.1--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2+++ b/.stash/scripts/port-scan.sh	Wed Jun 19 19:27:14 2024 +0000
     1.3@@ -0,0 +1,55 @@
     1.4+#!/bin/bash
     1.5+#Performs port scan using nmap
     1.6+
     1.7+print_usage() {
     1.8+cat << _EOF_
     1.9+        Utility to scan open ports. Can be used to scan ports for a domain or a list of domains specified in a file.
    1.10+        Example Usage:
    1.11+                -h, --help              Show brief help
    1.12+                -d, --domain            Domain name or ip to scan
    1.13+                -f, --file              Spefify a file containing domains/IPs to scan
    1.14+_EOF_
    1.15+}
    1.16+
    1.17+scan_port() {
    1.18+        domain=$1
    1.19+        echo "Scanning ports for $1...."
    1.20+        nmap -sT -T4 $domain | sed '/^\(Nmap scan\|PORT\|[0-9]\)/!d' | tee -a $port_scan_result_file
    1.21+}
    1.22+
    1.23+create_port_scan_result_file() {
    1.24+        port_scan_result_file="/tmp/port-scan-`date "+%Y-%m-%d-%H:%M:%S"`.txt"
    1.25+	touch $port_scan_result_file
    1.26+}
    1.27+
    1.28+while getopts "f:d:" opt; do
    1.29+        case "$opt" in
    1.30+                d) domain=$OPTARG    ;;
    1.31+                f) file=$OPTARG      ;;
    1.32+                *) print_usage; exit 1 ;;
    1.33+        esac
    1.34+done
    1.35+
    1.36+if [ ! -n "$domain" ] && [ ! -f "$file" ]; then
    1.37+        echo "Option -d $domain or -f $file missing or designates to wrong entry" >&2
    1.38+        exit 1
    1.39+fi
    1.40+
    1.41+scan_port_flow() {
    1.42+
    1.43+if [ -n "$domain" ]; then
    1.44+	create_port_scan_result_file
    1.45+	scan_port $domain
    1.46+	echo "Scan result:$port_scan_result_file"
    1.47+fi
    1.48+
    1.49+if [ -n "$file" ]; then
    1.50+	create_port_scan_result_file
    1.51+	for domain in $(cat $file)
    1.52+	do
    1.53+		scan_port $domain
    1.54+	done
    1.55+	echo "Scan result: $port_scan_result_file"
    1.56+fi
    1.57+}
    1.58+scan_port_flow