Nikhil.J Nikhil.J - 5 months ago 20
Linux Question

Can anyone help me to write a bash script to list all ip address in network and save in a file?

Can anyone help me in bash script to list all ip address in network and save in a file?

In need a script that list all the ip address in network after this i want make a ssh connection with each ip address and make some automated changes in rsyslog.conf.
Thanks!

typeset -i2 mask=255

[[ $# != 2 ]] && {
echo "Usage: $0 ipaddress subnetmask"
exit 1
}

SaveIFS=$IFS

IFS=.

typeset -a IParr=($1)
typeset -a NMarr=($2)

IFS=$SaveIFS

typeset -i2 ipbin1=${IParr[0]}
typeset -i2 ipbin2=${IParr[1]}
typeset -i2 ipbin3=${IParr[2]}
typeset -i2 ipbin4=${IParr[3]}
typeset -i2 nmbin1=${NMarr[0]}
typeset -i2 nmbin2=${NMarr[1]}
typeset -i2 nmbin3=${NMarr[2]}
typeset -i2 nmbin4=${NMarr[3]}

echo
echo " IP Address: $1"
echo " Subnet Mask: $2"
echo " Network Address: $((ipbin1 & nmbin1)).$((ipbin2 & nmbin2)).$((ipbin3 & nmbin3)).$((ipbin4 & nmbin4))"
echo "Broadcast Address: $((ipbin1 | (mask ^ nmbin1))).$((ipbin2 | (mask ^ nmbin2))).$((ipbin3 | (mask ^ nmbin3))).$((ipbin4 | (mask ^ nmbin4)))"
echo

exit 0

Answer Source
 nmap -sn $1"/"$2 | awk '/report for/ { print $5 }' > ipadds

This will give you an easy list of all the active hosts on the network using a given ip address and sbnet prefix.