cmd_loop.sh

#!/bin/bash
#------------------------------------------------------------------------------
# Name        : cmd_loop.sh
# Description : Run a command on a list of IP addresses.
# Author      : Lisandre.com
# Date        : 2018-09-28
# Prereq      : ~/config/IPs.txt with the list of IP addresses
#------------------------------------------------------------------------------

IP_FILE="~/config/IPs.txt"
RESULTS="~/results"

# For each IP in file, run command
# Ignore empty lines or commented lines
for line in $(cat "$IP_FILE" | grep -v -e '^$' | grep -v -e "^#"); do
    IP="`echo $line | cut -d ':' -f 1`"
    servername="`echo $line | cut -d ':' -f 2`"

    echo "-------------------------------------------"
    echo "Running command on server $servername ($IP)"
    echo "-------------------------------------------"
    nmap -A -v -T4 $IP > $RESULTS/nmap_${servername}_$(date +%Y%m%d_%H%M%S).txt
done