Created
August 28, 2012 20:48
-
-
Save dvliman/3504176 to your computer and use it in GitHub Desktop.
scan port
This file contains hidden or 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
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
copyright http://www.catonmat.net/blog/tcp-port-scanner-in-bash/