-1

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 Answers2

0

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
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • How does this handle byte overflows in the individual bytes? If you want to sum ip addresses you need to first transfer them from their ascii representation to numbers, sum them and transfer them back to ascii. Additionally you need to make sure that the result is a valid ip address that can be assigned to a host. [Here](http://stackoverflow.com/a/3222521/171318) you can find implementations of `INET_ATON` and `INET_NTOA` in bash, but I think the OP doesn't want to sum them at all (the [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)) – hek2mgl Jun 07 '15 at 12:41
  • Ok sorry, I was writing my own and did not see your answer. that it's far better than mine. thank you!! – tarzanello666 Jun 07 '15 at 12:52
  • My simple example is missing the functions hek2mgl mentioned. – Cyrus Jun 07 '15 at 12:58
0

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