how to sum ip addresses

1.3k views Asked by At

I'm creating a *.deb package that transform your wireless card into an hotspot. I'm stuck at the configurations: I have to write a postinst file in which I ask to the user what ip address he likes for his hotspot and then use it to generate the range & the subnet addresses for the isc-dhcp-server.

Something like that:

10.10.0.01 + 0.0.0.9 = 10.10.0.10

I know how to assign strings and numbers to variables and how to ask to user his choosen IP, but how to modify a variable and assign the result to another one? expr thinks it's a floating number and won't work.

Hoping that everything it's clear enough, waiting for a help,

thank you in advance

2

There are 2 answers

3
Cyrus On BEST ANSWER

Avoid leading zeros.

IFS="." read -a a <<< 10.10.0.1
IFS="." read -a b <<< 0.0.0.9
s="$[a[0]+b[0]].$[a[1]+b[1]].$[a[2]+b[2]].$[a[3]+b[3]]"
echo $s

Output:

10.10.0.10
0
tarzanello666 On

Ok, I found a workaround method:

when I ask to the user its choosen ip I use these:

IFS="." read -r a b c d
choosenip="$a.$b.$c.$d"
subnetip="$a.$b.$c.0" 
rangeipmin="$a.$b.$c.20"
rangeipmax="$a.$b.$c.30"

IFS change the default "space" or "tab" to whatever you want.

So when I have to put these in the dhcpd.conf with "echo", I just have to call the variables.

If you have more elegant ways to do that, you're welcome.

Thank you