ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi all,
Total site newbie, but been working on unix for 12 years, but is no longer my primary skillset, so getting rusty.
I am trying to create a bash script to enumerate a series of ip ranges, to individual IP addresses, so i can process them later.
For example; i want to be able to input a start IP and and end IP (using the usual $1 $2 script variables), i then want the script to list all the ip addresses one by one. So if i ran;
# myscript.sh 192.168.1.1 192.168.1.35
it would output;
192.168.10.1.1
192.168.10.1.2
192.168.10.1.3
192.168.10.1.5 ...... etc to .35
now i could work this out quite simply given some time, but the fact i then have to cater for more complex inputs, means i thought i had better ask for help here.
An complex example; # myscript.sh 10.1.1.1 10.2.5.7
so would need all IP's between this range, so;
10.1.1.1
10.1.1.2
10.1.1.3 ..... etc until
10.1.1.254
10.1.2.1
10.1.2.2 ..... etc until
10.1.254.254
10.2.1.1
10.2.1.2 ...... etc until end
Hope you get the idea, i would appreciate any help on how to achieve this.
So, essentially you want to convert the given IP addresses to their corresponding 32 bit integers, then loop through in between them and then convert the loop iterator back into IP addresses? I can't help you with the actual scripting because I don't bash that well, but I could probably code in C that program Real Quick (c).
ghostdog74 - that works, but i have no python knowledge, so how would i then change it to allow the script to be run with the startip and endip variables passed on the command line; eg;
I took the liberty of editing a handy little scanmac script which a programming guru made for me many moons ago in another world.
Code:
#!/bin/bash
#Example: ./myscript 192.168.0.1 192.168.0.10
if [ -z "$1" -o "$1" == "-?" ] ; then
echo "Usage: ./myscript starting-ip ending-ip"
exit 0
fi
startw=$(echo "$1" | cut -d. -f1)
startx=$(echo "$1" | cut -d. -f2)
starty=$(echo "$1" | cut -d. -f3)
startz=$(echo "$1" | cut -d. -f4)
echo "Starting address: $startw.$startx.$starty.$startz"
end_ip="$2"
if [ -z "$end_ip" ] ; then
if [ $startz -eq 255 ] ; then
endz=0
if [ $starty -eq 255 ] ; then
endy=0
if [ $startx -eq 255 ] ; then
endx=0
if [ $startw -eq 255 ] ; then
echo "255.255.255.255 is not valid"
exit 1
else
endw=$(($startw+1))
fi
else
endx=$(($startx+1))
fi
else
endy=$(($starty+1))
fi
else
endz=$(($startz+1))
fi
end_ip="$endw.$endx.$endy.$endz"
fi
echo "Ending address: $end_ip"
currentw=$startw
currentx=$startx
currenty=$starty
currentz=$startz
currentip="$currentw.$currentx.$currenty.$currentz"
#if [ "$currentz" -ne "0" ] ; then
# echo "$currentip"
#fi
trap "exit 15" 15
trap "exit 2" 2
while [ "$currentip" != "$end_ip" ] ; do
currentip="$currentw.$currentx.$currenty.$currentz"
# if [ "$currentz" -eq "0" ] ; then
# echo "$currentip"
# fi
echo $currentip
if [ $currentz -eq 255 ] ; then
currentz=0
else
currentz=$(($currentz+1))
continue
fi
if [ $currenty -eq 255 ] ; then
currenty=0
else
currenty=$(($currenty+1))
continue
fi
if [ $currentx -eq 255 ] ; then
currentx=0
else
currentx=$(($currentx+1))
continue
fi
if [ $currentw -eq 255 ] ; then
break
else
currentz=$(($currentz+1))
continue
fi
done
Here is the original scanmac script.
Code:
#!/bin/bash
if [ -z "$1" -o "$1" == "-?" ] ; then
echo "Usage: scanmac starting-ip [ending-ip]"
echo
echo "Will list IP and MAC adresses of all active computers"
echo "within a physical network segment."
echo
echo "If ending-ip is omitted, ending-ip = starting-ip+1"
echo " (so that only the host specified will be scanned)."
echo
echo "If you wish to scan from 192.168.2.1 to 192.168.2.31,"
echo " inclusive, specify ending-ip to be 192.168.2.32."
echo
exit 0
fi
startw=$(echo "$1" | cut -d. -f1)
startx=$(echo "$1" | cut -d. -f2)
starty=$(echo "$1" | cut -d. -f3)
startz=$(echo "$1" | cut -d. -f4)
echo "Starting address: $startw.$startx.$starty.$startz"
endip="$2"
if [ -z "$endip" ] ; then
if [ $startz -eq 255 ] ; then
endz=0
if [ $starty -eq 255 ] ; then
endy=0
if [ $startx -eq 255 ] ; then
endx=0
if [ $startw -eq 255 ] ; then
echo "Sorry, you cannot just scan 255.255.255.255."
echo " Maybe later, or you can try to hack support in."
exit 1
else
endw=$(($startw+1))
fi
else
endx=$(($startx+1))
fi
else
endy=$(($starty+1))
fi
else
endz=$(($startz+1))
fi
endip=$endw.$endx.$endy.$endz
fi
echo "Ending address: $endip"
currentw=$startw
currentx=$startx
currenty=$starty
currentz=$startz
currentip=$currentw.$currentx.$currenty.$currentz
# If the bottom octet is zero, it'll get logged inside the while, so
# don't do it here
if [ "$currentz" -ne "0" ] ; then
echo "$(date): $currentip"
fi
trap "exit 15" 15
trap "exit 2" 2
while [ "$currentip" != "$endip" ] ; do
currentip=$currentw.$currentx.$currenty.$currentz
# another log message whenever the bottom octet rolls over
if [ "$currentz" -eq "0" ] ; then
echo "$(date): $currentip"
fi
ping -c 1 -w 10 $currentip >/dev/null 2>&1
mac=$(arp -a $currentip | cut -d' ' -f4)
case "$mac" in
?incomplete?)
: # do nothing -- no MAC for this IP
;;
entries)
: # again, do nothing -- this is a broadcast address
;;
*)
echo "IP: $currentip -- MAC: $mac"
;;
esac
mac="" # reset for the next loop iteration
# now increment the ip values (w,x,y,z)
if [ $currentz -eq 255 ] ; then
currentz=0
else
currentz=$(($currentz+1))
continue
fi
if [ $currenty -eq 255 ] ; then
currenty=0
else
currenty=$(($currenty+1))
continue
fi
if [ $currentx -eq 255 ] ; then
currentx=0
else
currentx=$(($currentx+1))
continue
fi
if [ $currentw -eq 255 ] ; then
break
else
currentz=$(($currentz+1))
continue
fi
done
ghostdog74 - that works, but i have no python knowledge, so how would i then change it to allow the script to be run with the startip and endip variables passed on the command line; eg;
Homey - Thanks for that, works a treat, will make some mods to remove the .0 and .255 out of the ranges though.
As to the scanmac tool, i wrote a similar one a while ago, but mine was just a few lines as it simply pinged the subnet mask, which caused all the local ips in use to reply, then used the arp command to list all the new known ones out.
I also wrote a script to work with this and samba, for linux.
Which then took the list of alive ip addreses and polled them to see who was logged into them (winXP clients). Worked a treat.
It turns out my first attempt made things more difficult than they really needed to be. Anyway, here's a bash script that removes the 0 and 255 addresses.
Code:
#!/bin/bash
start_ip=$1
end_ip=$2
counter[0]=$( echo ${start_ip} | cut -f 1 -d . )
counter[1]=$( echo ${start_ip} | cut -f 2 -d . )
counter[2]=$( echo ${start_ip} | cut -f 3 -d . )
counter[3]=$( echo ${start_ip} | cut -f 4 -d . )
while [ "${end_ip}" != "${counter[0]}.${counter[1]}.${counter[2]}.${counter[3]}" ] ; do
if [ \( ${counter[3]} -ne 0 \) -a \
\( ${counter[3]} -ne 255 \) ] ; then
echo "${counter[0]}.${counter[1]}.${counter[2]}.${counter[3]}"
fi
let counter[3]=${counter[3]}+1
if [ ${counter[3]} -eq 256 ] ; then
counter[3]=0
let counter[2]=${counter[2]}+1
fi
if [ ${counter[2]} -eq 256 ] ; then
counter[2]=0
let counter[1]=${counter[1]}+1
fi
if [ ${counter[1]} -eq 256 ] ; then
counter[1]=0
let counter[0]=${counter[0]}+1
fi
done
echo "${end_ip}"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.