Skip to content

Instantly share code, notes, and snippets.

@Diaa-Hassan
Forked from priyanshus/port-scan.sh
Created June 29, 2022 03:37
Show Gist options
  • Select an option

  • Save Diaa-Hassan/d7d3d42f7fbe3131f5768ef3ecd48c44 to your computer and use it in GitHub Desktop.

Select an option

Save Diaa-Hassan/d7d3d42f7fbe3131f5768ef3ecd48c44 to your computer and use it in GitHub Desktop.

Revisions

  1. @priyanshus priyanshus revised this gist Nov 23, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion port-scan.sh
    Original file line number Diff line number Diff line change
    @@ -51,6 +51,5 @@ if [ -n "$file" ]; then
    done
    echo "Scan result: $port_scan_result_file"
    fi
    #cat $port_scan_result_file | sed '/^\(Nmap scan\|PORT\|[0-9]\)/!d' >> $port_scan_result_file
    }
    scan_port_flow
  2. @priyanshus priyanshus created this gist Nov 23, 2019.
    56 changes: 56 additions & 0 deletions port-scan.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    #!/bin/bash
    #Performs port scan using nmap

    print_usage() {
    cat << _EOF_
    Utility to scan open ports. Can be used to scan ports for a domain or a list of domains specified in a file.
    Example Usage:
    -h, --help Show brief help
    -d, --domain Domain name or ip to scan
    -f, --file Spefify a file containing domains/IPs to scan
    _EOF_
    }

    scan_port() {
    domain=$1
    echo "Scanning ports for $1...."
    nmap -sT -T4 $domain | sed '/^\(Nmap scan\|PORT\|[0-9]\)/!d' | tee -a $port_scan_result_file
    }

    create_port_scan_result_file() {
    port_scan_result_file=port-scan-`date "+%Y-%m-%d-%H:%M:%S"`.txt
    touch $port_scan_result_file
    }

    while getopts "f:d:" opt; do
    case "$opt" in
    d) domain=$OPTARG ;;
    f) file=$OPTARG ;;
    *) print_usage; exit 1 ;;
    esac
    done

    if [ ! -n "$domain" ] && [ ! -f "$file" ]; then
    echo "Option -d $domain or -f $file missing or designates to wrong entry" >&2
    exit 1
    fi

    scan_port_flow() {

    if [ -n "$domain" ]; then
    create_port_scan_result_file
    scan_port $domain
    echo "Scan result:$port_scan_result_file"
    fi

    if [ -n "$file" ]; then
    create_port_scan_result_file
    for domain in $(cat $file)
    do
    scan_port $domain
    done
    echo "Scan result: $port_scan_result_file"
    fi
    #cat $port_scan_result_file | sed '/^\(Nmap scan\|PORT\|[0-9]\)/!d' >> $port_scan_result_file
    }
    scan_port_flow