Skip to content

Instantly share code, notes, and snippets.

@dmknght
Last active December 11, 2023 22:53
Show Gist options
  • Save dmknght/d1517865a41c47608b14c26e6bb2c16b to your computer and use it in GitHub Desktop.
Save dmknght/d1517865a41c47608b14c26e6bb2c16b to your computer and use it in GitHub Desktop.
A port scanner in bash. No netcat / nmap is required. Might be useful when discover open ports in internal network on a Linux server.
#!/bin/bash
# Example of using bash with array
port_arr=(80 22 3306)
max_timeout=2 # Timeout requires coreutils (on Debian-based system)
function do_scan_port {
# If use array like above, use the line above
for port in "${port_arr[@]}"; do
# Otherwise, use the port range
# for port in $(seq 9990 10000); do
# Check open ports
{ timeout $max_timeout bash -c "echo \"\" >/dev/tcp/$1/$port && echo \"Open port: ${1}:${port}\"" ;} 2>/dev/null
done
}
do_scan_port google.com
# Example of using bash for IP range
# 172.16.1.3/24
#for ip in 172.16.1.{1..254}; do
# do_scan_port $ip
#done
# 172.16.1.3/20
#for ip in 10.11.{48..63}.{1..254}; do
# do_scan_port $ip
#done
@dmknght
Copy link
Author

dmknght commented Dec 11, 2023

Update time out so script wont hang.

@dmknght
Copy link
Author

dmknght commented Dec 11, 2023

Add & to end of line of scan command could running multiple processes scanning the host?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment