Question

I began recently to work with UNIX systems. I've created a little Project for my RaspPI and me.

I stuck on a point where i didn't find any help on google etc. My Question is:

Is it possible to add the IP adress in a variable with the CIDR notification? I finally got the IP adress in a variable but can't get the CIDR notification. My Plan was to create a script, where i compare my current netmask with a list of netmasks. It's like this :

NETMASK=/sbin/ifconfig eth0 | grep Mask | cut -d":" -f4 CIDR= 0 if [[ $NETMASK -eq '255.255.255.0' ]] then CIDR=24 elif [[ $NETMASK -eq '255.255.255.128' ]] then CIDR=25 fi The output of this File says : "Whole Integer expression estimated" (Translated from german into english".

Does any1 know how i have to compare the Netmask or knows another way to get the CIDR notfication, based on the actual netmask?(The Netmask should be changeable)

thank you for any further help!

Cheers

Maesi

Was it helpful?

Solution

Let ip and awk do the work:

CIDR=`/sbin/ip address show eth0|awk '/inet / {split($2, a, "/"); print a[2]}'`

OTHER TIPS

WOW! Thanks... that's a much comfortable solution as I had. Instead of 32 IF comparisons i can use your command!

@BTW

After looking for the solution I finally found it on the IF comparison.

TO compare the IP's i had to use " = " as operator and not " -eq " .

-eq (Integer) = (String)

Thank you anyway! I will use your command, it's easier instead of mine.

Cheers Maesi

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top