changelog shortlog graph tags branches changeset files revisions annotate raw help

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

changeset 48: 953c5127546a
parent: stash/scripts/port-scan.sh@45f3b1bc3cd7
author: Richard Westhaver <ellis@rwest.io>
date: Wed, 19 Jun 2024 19:27:14 +0000
permissions: -rwxr-xr-x
description: stash -> .stash
1 #!/bin/bash
2 #Performs port scan using nmap
3 
4 print_usage() {
5 cat << _EOF_
6  Utility to scan open ports. Can be used to scan ports for a domain or a list of domains specified in a file.
7  Example Usage:
8  -h, --help Show brief help
9  -d, --domain Domain name or ip to scan
10  -f, --file Spefify a file containing domains/IPs to scan
11 _EOF_
12 }
13 
14 scan_port() {
15  domain=$1
16  echo "Scanning ports for $1...."
17  nmap -sT -T4 $domain | sed '/^\(Nmap scan\|PORT\|[0-9]\)/!d' | tee -a $port_scan_result_file
18 }
19 
20 create_port_scan_result_file() {
21  port_scan_result_file="/tmp/port-scan-`date "+%Y-%m-%d-%H:%M:%S"`.txt"
22  touch $port_scan_result_file
23 }
24 
25 while getopts "f:d:" opt; do
26  case "$opt" in
27  d) domain=$OPTARG ;;
28  f) file=$OPTARG ;;
29  *) print_usage; exit 1 ;;
30  esac
31 done
32 
33 if [ ! -n "$domain" ] && [ ! -f "$file" ]; then
34  echo "Option -d $domain or -f $file missing or designates to wrong entry" >&2
35  exit 1
36 fi
37 
38 scan_port_flow() {
39 
40 if [ -n "$domain" ]; then
41  create_port_scan_result_file
42  scan_port $domain
43  echo "Scan result:$port_scan_result_file"
44 fi
45 
46 if [ -n "$file" ]; then
47  create_port_scan_result_file
48  for domain in $(cat $file)
49  do
50  scan_port $domain
51  done
52  echo "Scan result: $port_scan_result_file"
53 fi
54 }
55 scan_port_flow