I need help with a shell script! I want to prompt someone to enter an IP address the automatically increment it and output the sequence of that address and its following addresses, but I don’t know how to do that: when I try to increment a value like “10.10.10.27” it fails because it’s not a number.
You’ve realized the problem already, you just need to think slightly out of the box to figure out the solution. That is, if an IP address isn’t a number from the perspective of the shell, you need to break it up so that it is.
But first, a quick primer: IP (v4, at least) addresses are comprised of four decimal values, each ranging from 0 to 255. Effectively, however, 0 and 255 are typically reserved for routers, loopback addresses, etc, so really you’ll want to range from 1-254 (but that’s easy to tweak anyway).
A given IP address is therefore four numeric values all strung together with dot separators: 10.10.10.27. What the script needs to do is split the address back up, which can be done a number of different ways, but I’ll use the cut command since I can specify what field separator to use:
baseaddr="$(echo $ip | cut -d. -f1-3)"
lsv="$(echo $ip | cut -d. -f4)"
(I’m using “lsv” to represent the least significant value here).
With this snippet of code, you’ll have the following two values set:
baseaddr = 10.10.10
lsv = 27
That’s perfect. Now you can step through your loop incrementing the “lsv” variable until you hit your MaxValue (254 or 255), regardless of where you start.
Here’s my full version of your script:
#!/bin/sh
MaxValue=255 # highest valid IP octet value
echo -n "Enter IP address: "; read ip
echo -n "How many IP addresses do you need: "; read count
baseaddr="$(echo $ip | cut -d. -f1-3)"
lsv="$(echo $ip | cut -d. -f4)"
while [ $count -gt 0 ]
do
if [ $lsv -eq $MaxValue ] ; then
# here you'll need to increment the third level IP value,
# but that might cascade into the second, or the first.
# consider the case of 17.255.255.255 + 1
echo "edge case needs to be written"
fi
echo $baseaddr.$lsv
lsv=$(( $lsv + 1 ))
count=$(( $count - 1 ))
done
exit 0
Note that there’s an important edge condition that needs to be written, which will actually make this script quite a bit more complex. The situation you need to tackle is what happens when the IP address rolls past the MaxValue?
If you have 10.10.10.255 then the next value can’t be 10.10.10.256 because that’s no longer a valid IP address, but rather 10.10.11.0. Doable, but you’ll probably end up needing to break the IP address into all four octets rather than just the first three as a monolithic value and the fourth as the incrementing value.
But I’ll leave that part of this homework assignment to you. 🙂
Thank You. It helped me
echo 1.1.1.1 | awk -F. ‘{$4++}{gsub(OFS,”.”)}1’)
>1.1.1.2
Except for:
$ echo 1.1.1.256 | awk -F. ‘{$4++}{gsub(OFS,”.”)}1’
1.1.1.257
thanks Dave. Yes i am able to increment it .sample output below .
[14:15:49 root@ieatlms5684:scripts ]# ./ip_address.sh
enter the ip address
253.253.253.253
enter the incremental count
10
253.253.253.253
253.253.253.254
253.253.254.255
253.253.255.255
253.254.255.255
253.255.255.255
254.255.255.255
255.255.255.255
Ah, I assume you’re saying that there’s a bug in the script here? You raise a good edge case, one that would require a lot more code to fix. How about this: You take a shot at it, post your revised code here and I’ll give it a look? 🙂
Did my homework and was able to write a script to go to 1000 contiguous IP address. Without this script probably would have never figured this out so thanks Dave.
The one given by Dave is a nice one 🙂 .
Without considering the decimal of last octet exceeding the max value 255 (as Dave stated above)
I got a very small one liner for that: (my one is not taking care of that)
[jsaikia@zorro trap]$ cat ips.con
172.22.22.11
192.168.32.6
172.18.1.121
172.20.21.21
[jsaikia@zorro trap]$ for ip in `cat ips.con`; do echo $ip | awk -F “.” ‘{print $1″.”$2″.”$3″.”$4+1}’; done
172.22.22.12
192.168.32.7
172.18.1.122
172.20.21.22