Created
April 21, 2022 12:12
-
-
Save boogheta/455d1ca4507860fe9379fe7c2676da74 to your computer and use it in GitHub Desktop.
Scan open ports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alarm() { | |
perl -e ' | |
eval { | |
$SIG{ALRM} = sub { die }; | |
alarm shift; | |
system(@ARGV); | |
}; | |
if ($@) { exit 1 } | |
' "$@"; | |
} | |
scan() { | |
if [[ -z $1 || -z $2 ]]; then | |
echo "Usage: $0 <host> <port, ports, or port-range>" | |
return | |
fi | |
local host=$1 | |
local ports=() | |
case $2 in | |
*-*) | |
IFS=- read start end <<< "$2" | |
for ((port=start; port <= end; port++)); do | |
ports+=($port) | |
done | |
;; | |
*,*) | |
IFS=, read -ra ports <<< "$2" | |
;; | |
*) | |
ports+=($2) | |
;; | |
esac | |
for port in "${ports[@]}"; do | |
alarm 1 "echo >/dev/tcp/$host/$port && | |
echo \"port $port is open\"" || | |
echo "port $port is closed" | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment