-
Notifications
You must be signed in to change notification settings - Fork 17
/
portscan.sh
85 lines (57 loc) · 1.39 KB
/
portscan.sh
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
#!/bin/bash
IPV4REGEX="^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"
USAGE="Syntax: $0 [-h] -n <network> -p <int32 ports>
OsbornePro portscan 1.1 ( https://osbornepro.com )
Usage: portscan -n <string format is #.#.#> -p [int array]
OPTIONS:
-h : Displays the help information for the command
-n : Set the hostname or ip address to test for open ports on
-p : Set the ports to check
-t : Set the timeout value for testing connections
EXAMPLES:
portscan -n 192.168.0.1 -p 22 -t 1
# This examples tests for port 22 being open on 192.168.0.1 using a 1 second timeout
"
function allow_ctrlc {
# Allow Ctrl+C to kill pingsweep
trap '
trap - INT # restore default INT handler
kill -s INT "$$"
' INT
} # End function allow_ctrlc
function print_usage {
printf "$USAGE\n" >&2
exit 1
} # End function print_usage
function validate_port {
if ((65535 <= $port)); then
printf "[x] Port needs to be between 1 and 65535\n"
exit 1
fi
} # End function validate_port
function test_port {
(timeout $timeout bash -c "cat < /dev/null > /dev/tcp/$ipv4/$port") && printf "[*] Port $port is open on $ipv4\n"
} # End function test_port
while [ ! -z "$1" ]; do
case "$1" in
-n)
shift
ipv4=$1
;;
-p)
shift
port=$1
;;
-t)
shift
timeout=$1
;;
*)
print_usage
;;
esac
shift
done
allow_ctrlc
validate_port
test_port