Skip to content

Instantly share code, notes, and snippets.

@boogheta
Created April 21, 2022 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boogheta/455d1ca4507860fe9379fe7c2676da74 to your computer and use it in GitHub Desktop.
Save boogheta/455d1ca4507860fe9379fe7c2676da74 to your computer and use it in GitHub Desktop.
Scan open ports
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