├── .gitignore ├── README.md └── scanner /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | bash-port-scanner 2 | ================= 3 | 4 | Port scanner in bash (TCP) 5 | 6 | Usage 7 | ----- 8 | 9 | First, source the functions: 10 |
. scanner
11 | 12 | Execute the scan with the following syntax: 13 |
scan <host> <port, ports, port-range>
14 | 15 | Examples: 16 |
17 | scan google.com 80
18 | 
19 | scan google.com 78-82
20 | 
21 | scan google.com 25,80,443
22 | 
23 | 24 | Output 25 | ------ 26 |
27 | user@host:~/bash-port-scanner$ . scanner
28 | user@host:~/bash-port-scanner$ scan google.com 78-82
29 | 78/tcp closed
30 | 79/tcp closed
31 | 80/tcp open
32 | 81/tcp closed
33 | 82/tcp closed
34 | user@host:~/bash-port-scanner$ scan google.com 25,80,443
35 | 25/tcp closed
36 | 80/tcp open
37 | 443/tcp open
38 | 
39 | 40 | Background 41 | ---------- 42 | Original concept found here: http://www.catonmat.net/blog/tcp-port-scanner-in-bash/ 43 | 44 | I made a couple tweaks in the formatting. I plan to abstract it a little further to incorporate UDP. 45 | -------------------------------------------------------------------------------- /scanner: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # +----------------------------------------------------------------------------+ 4 | # | Name: scanner | 5 | # | | 6 | # | Purpose: Port scanner (TCP) implemented in bash | 7 | # | | 8 | # | Date Created: 2012/09/16 | 9 | # | Date Modified: 2013/11/08 | 10 | # | | 11 | # | Usage: source this file first: . scanner | 12 | # | scan | 13 | # | | 14 | # | Example: scan google.com 80 | 15 | # | scan google.com 78-82 | 16 | # | scan google.com 25,80,443 | 17 | # +----------------------------------------------------------------------------+ 18 | 19 | # +----------------------------------------------------------------------------+ 20 | # | function alarm | 21 | # | | 22 | # | Description: waits $1 amount of seconds before killing $2 | 23 | # | | 24 | # | Arguments: | 25 | # | $1: number of seconds | 26 | # | $2: command to execute in bash shell | 27 | # | | 28 | # | Returns: | 29 | # | 0 if command had to be killed (if port is open, process will be killed) | 30 | # | not a 0 if command wasn't killed (if port is closed, process dies) | 31 | # +----------------------------------------------------------------------------+ 32 | alarm() { 33 | local timeout=$1; shift; 34 | # execute command, store PID 35 | bash -c "$@" & 36 | local pid=$! 37 | # sleep for $timeout seconds, then attempt to kill PID 38 | { 39 | sleep "$timeout" 40 | kill $pid 2> /dev/null 41 | } & 42 | wait $pid 2> /dev/null 43 | return $? 44 | } 45 | 46 | # +----------------------------------------------------------------------------+ 47 | # | function scan | 48 | # | | 49 | # | Description: attempts to write to /dev/tcp/$1/$2; if write is successful, | 50 | # | port is open. | 51 | # | | 52 | # | Arguments: | 53 | # | $1: host or IP | 54 | # | $2: port(s) or port range | 55 | # | | 56 | # | Returns: | 57 | # | Nothing | 58 | # +----------------------------------------------------------------------------+ 59 | scan() { 60 | if [[ -z $1 || -z $2 ]]; then 61 | echo "Usage: scan " 62 | echo "Example: scan google.com 79-81" 63 | return 64 | fi 65 | 66 | local host=$1 67 | local ports=() 68 | # store user-provided ports in array 69 | case $2 in 70 | *-*) 71 | IFS=- read start end <<< "$2" 72 | for ((port=start; port <= end; port++)); do 73 | ports+=($port) 74 | done 75 | ;; 76 | *,*) 77 | IFS=, read -ra ports <<< "$2" 78 | ;; 79 | *) 80 | ports+=($2) 81 | ;; 82 | esac 83 | 84 | # attempt to write to each port, print open if successful, closed if not 85 | for port in "${ports[@]}"; do 86 | alarm 1 "echo >/dev/tcp/$host/$port" && 87 | echo "$port/tcp open" || 88 | echo "$port/tcp closed" 89 | done 90 | } 91 | --------------------------------------------------------------------------------