Jump to content

Hi,

#! /bin/bash
ip="172.16.0.1"
networkconf="filetext.txt"
dhcpd="filetext.txt"
netmask30="2"
netmask29="6"
netmask28="14"
echo "How many raspberry pi's are on your premises?"
        read norpi
if [[ "$norpi" -le "$netmask30" ]]; then
        sed -i "30s/.*/netmask 255.255.255.252/" "$networkconf"
        sed -i.bak "10s/$/ 255.255.255.252{/" "$dhcpd"
elif [[ "$norpi" -ge 3 && "$norpi" -le "$netmask29" ]]; then
        sed -i "30s/.*/netmask 255.255.255.248/" "$networkconf"
        sed -i.bak "10s/$/ 255.255.255.248{/" "$dhcpd"
elif [[ "$norpi" -ge 7 && "$norpi" -le "$netmask28" ]]; then
        sed -i "30s/.*/netmask 255.255.255.240/" "$networkconf"
        sed -i.bak "10s/$/ 255.255.255.240{/" "$dhcpd"
else
        echo "unable"
fi

lastoct=${ip: -1}
range="$(($lastoct + $norpi))"
echo "$range"

So at the moment I'm able to take the last digit of the ip and add the amount of raspberry pi's, but what I want to do know is to place the new digit, from variable 'range' and replace the last digit of 'ip' with 'range'.

 

Any Suggestions?

Thank you

Link to post
Share on other sites

Its a bit tricky with different subnet ranges, so i dont know how much you want to get into that but here is a start:

a=(${ip//./ })

will give to an array with all the numbers between the dots

you can access each number with

${a[0]}

${a[1]}

etc.

Then you can do what you want to do with the range. If I take my interpretation of your question it would be:

a=(${ip//./ })

range="$(($lastoct + $norpi))"

newRange=$(($range+${a[3]}))

echo "${a[0]}.${a[1]}.${a[2]}.$newRange"

 

EDIT corrected wrong bracket type

             ☼

ψ ︿_____︿_ψ_   

Link to post
Share on other sites

23 hours ago, SCHISCHKA said:

a={${ip//./ })

Shouldn't this be

 

a=(${ip///./})

 

to make an array out of it?

AMD Ryzen 7800 X3D, MSI B650 Project Zero, Antec C5, Gigabyte RTX 4080 Super Aero

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to post
Share on other sites

16 minutes ago, Fetzie said:

Shouldn't this be

 

a=(${ip///./})

 

to make an array out of it?

I did incorrectly use a { instead of a (

You can try a=(${ip//./})

but that just results in
echo ${a[0]}
1721601

Any other symbol i try other than a space "/ " doesn't work. I dont know why this is the case. Its just something iv always done and never thought about questioning it.

 

             ☼

ψ ︿_____︿_ψ_   

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×