###################################################
# Script Name : noOfHosts
# Created By : Jino Joseph
# Created Date : 08-Jul-2014
# Last Modified : 08-Jul-2014
# Purpose : To find the Total number of Hosts between two ip addresses.
###################################################
#!/bin/bash
sed -i 's/\t/#/g' ips.txt
for i in `cat /root/ips.txt`; do
IPFROM=$(echo $i | awk -F '#' '{print $1}');
IPTO=$(echo $i | awk -F '#' '{print $2}');
python -c "
import ipaddress
import sys
ip1 = int(ipaddress.IPv4Address(unicode('"$IPFROM"')))
ip2 = int(ipaddress.IPv4Address(unicode('"$IPTO"')))
host = ip2 - ip1
Nhost = host + 1
sys.stdout.write(str(Nhost))
"
echo " $IPFROM $IPTO";
done
###################################################
This will give the result as "Number of IP" , "FromIP" , "ToIP".
Each lines of ips.txt file contains FromIP and ToIP seperated by a #
Setting up the Python environment for working the above:
###################################################
# yum install python-setuptools
# wget http://pypi.python.org/packages/source/p/pip/pip-0.7.2.tar.gz
# tar xzf pip-0.7.2.tar.gz
# cd pip-0.7.2
# python setup.py install
# pip install ipaddress
###################################################
# Script Name : noOfHosts
# Created By : Jino Joseph
# Created Date : 08-Jul-2014
# Last Modified : 08-Jul-2014
# Purpose : To find the Total number of Hosts between two ip addresses.
###################################################
#!/bin/bash
for i in `cat /root/ips.txt`; do
IPTO=$(echo $i | awk -F '#' '{print $2}');
import ipaddress
import sys
ip1 = int(ipaddress.IPv4Address(unicode('"$IPFROM"')))
ip2 = int(ipaddress.IPv4Address(unicode('"$IPTO"')))
host = ip2 - ip1
Nhost = host + 1
sys.stdout.write(str(Nhost))
"
echo " $IPFROM $IPTO";
done
This will give the result as "Number of IP" , "FromIP" , "ToIP".
Each lines of ips.txt file contains FromIP and ToIP seperated by a #
Setting up the Python environment for working the above:
###################################################
# yum install python-setuptools
# wget http://pypi.python.org/packages/source/p/pip/pip-0.7.2.tar.gz
# tar xzf pip-0.7.2.tar.gz
# cd pip-0.7.2
# python setup.py install
# pip install ipaddress
###################################################
Comments