Last active
August 10, 2024 01:50
-
-
Save KBeDevel/6c2c629a30d943c377a47db4b3dad6c2 to your computer and use it in GitHub Desktop.
Scan and save network hosts ports output autmatically. Needs nmap installed.
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
#! /usr/bin/bash | |
# Script to scan hosts ports using NMAP | |
# Created by: Benjamin Calisto <[email protected]> | |
check_nmap_installation() { | |
is_nmap_installed=$(command -v nmap > /dev/null && echo 1 || echo 0) | |
if [ "$is_nmap_installed" -eq 0 ] ; then | |
echo "nmap is not installed" | |
exit 1 | |
fi | |
} | |
check_network() { | |
regex="^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/([0-9]|[1-2][0-9]|3[0-2]))?$" | |
if [ "$1" = "" ] ; then | |
echo "No network target to scan. Example: 192.168.0.1/24" | |
exit 1 | |
fi | |
if [[ ! $1 =~ $regex ]] ; then | |
echo "Invalid network or subnet. Example: 192.168.0.1/24" | |
exit 1 | |
fi | |
} | |
{ | |
check_nmap_installation | |
check_network "$1" | |
} | |
addresses="$(sudo nmap -n -sn "$1" -oG - | awk '/Up$/{print $2}')" | |
current_timestamp="$(date '+%s')" | |
if [ ! -d "logs" ] ; then | |
mkdir "logs" | |
fi | |
echo "$addresses" > "logs/ips_dump_$current_timestamp.txt" | |
dump_file="logs/scan_dump_$current_timestamp.log.md" | |
{ | |
printf "# NMAP Scans\n\n" | |
echo "## Hosts summary" | |
printf "\n%s\n\n" "$( | |
while IFS= read -r addr | |
do | |
if [ "$addr" != "" ] ; then | |
echo "- $addr" | |
fi | |
done <<< "$addresses" | |
)" | |
printf "## Unit scans\n\n" | |
} >> "$dump_file" | |
code_block_wrapper_chars="\`\`\`" | |
# shellcheck disable=SC2183 | |
vertical_divider=$(printf "%*s" "$(tput cols)" | tr " " "-") | |
while IFS= read -r ipv4 | |
do | |
if [ "$ipv4" != "" ] ; then | |
{ | |
printf "### Results for host %s\n\n" "$ipv4" | |
printf "%s\n" "$code_block_wrapper_chars" | |
} >> "$dump_file" | |
sudo nmap -sV -O "$ipv4" | tee -a "$dump_file" | |
printf "\n%s\n\n" "$vertical_divider" | |
printf "%s\n\n" "$code_block_wrapper_chars" >> "$dump_file" | |
fi | |
done <<< "$addresses" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment