LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2013, 04:40 AM   #16
shiftee
LQ Newbie
 
Registered: Aug 2012
Posts: 3

Rep: Reputation: Disabled

Quote:
Originally Posted by David the H. View Post
Now that's the proper way to re-open a thread; making corrections and adding extra info.

Following that, here's a modification of the above function of my own. It's a bit shorter, but I couldn't say whether it's any more efficient.

Code:
#!/bin/bash

mask2cidr() {

    local nbits dec
    local -a octets=( [255]=8 [254]=7 [252]=6 [248]=5 [240]=4
                      [224]=3 [192]=2 [128]=1 [0]=''           )
    
    while read -rd '.' dec; do
        [[ -z ${octets[dec]} ]] && echo "Error: $dec is not recognised" && exit 1
        (( nbits += octets[dec] ))
        (( dec < 255 )) && break
    done <<<"$1."

    echo "/$nbits"

}

mask2cidr "$1"

exit 0
I had to do the following to get it to work:
- [0]=''
+ [0]=0

Test code:
Code:
for i in $(seq 0 255)
do
    MASK="255.255.$i.0"
    BITS=$(mask2cidr $MASK)
    [ $? -eq 0 ] && echo "$MASK   : $BITS"
done

for i in $(seq 1 255)
do
    MASK="255.255.255.$i"
    BITS=$(mask2cidr $MASK)
    [ $? -eq 0 ] && echo "$MASK : $BITS"
done
 
Old 12-13-2013, 07:41 AM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I guess it is only fair to update the awk:
Code:
#!/usr/bin/awk -f

BEGIN {
	bits = 8
	for(x = 255; x >=0; x -= 2^i++)
		cidr[x] = bits--

	split(ARGV[1], arr, "[.]")

	for(i = 1; i <= 4;i++){
		total += cidr[arr[i]]
		if(arr[i] < 255) break
	}

	print total
}
I also altered it to take the address as an argument on the command line
 
Old 02-22-2014, 09:35 AM   #18
Secmas
LQ Newbie
 
Registered: Oct 2011
Location: Country of the Ethernal Spring
Posts: 1

Rep: Reputation: Disabled
This is a really nice post, thanks to all that contributed in this post.

I am doing an script that checks IPTABLES to see if there are IPs that could be joined in one CIDR notation up to the /24 no more than that and wish there could be something that could do this, but in the mean time this code will help me a lot.

Thanks!
 
Old 10-29-2014, 10:13 AM   #19
nbritton
Member
 
Registered: Jun 2013
Location: Dubuque, IA
Distribution: Red Hat Enterprise Linux, Mac OS X, Ubuntu, Fedora, FreeBSD
Posts: 89

Rep: Reputation: Disabled
CIDR in base 10

For a class project I made a CIDR netmask calculator using base 10 math. I release this code into the public domain.

Code:
#!/bin/bash
# CIDR Netmask Calculator

y=$[2**(32-$1)] # y = 2^(32-x), x = CIDR class

printf "$1\t" # x
printf "$y\t" # y

if ! [ "$[$y/256**0]" -gt "256" ]; then		# y
	echo 255.255.255.$[256-($y/256**0)]	# 256-(y)
	exit
fi
if ! [ "$[$y/256**1]" -gt "256" ]; then		# y/256
	echo 255.255.$[256-($y/256**1)].0	# 256-(y/256)
	exit
fi
if ! [ "$[$y/256**2]" -gt "256" ]; then		# y/256/256
	echo 255.$[256-($y/256**2)].0.0		# 256-(y/256/256)
	exit
fi
if ! [ "$[$y/256**3]" -gt "256" ]; then		# y/256/256/256
	echo $[256-($y/256**3)].0.0.0		# 256-(y/256/256/256)
	exit
fi
Output:
Code:
$ for i in {0..32}; do ./cidr.sh $i; done
0	4294967296	0.0.0.0
1	2147483648	128.0.0.0
2	1073741824	192.0.0.0
3	536870912	224.0.0.0
4	268435456	240.0.0.0
5	134217728	248.0.0.0
6	67108864	252.0.0.0
7	33554432	254.0.0.0
8	16777216	255.0.0.0
9	8388608	255.128.0.0
10	4194304	255.192.0.0
11	2097152	255.224.0.0
12	1048576	255.240.0.0
13	524288	255.248.0.0
14	262144	255.252.0.0
15	131072	255.254.0.0
16	65536	255.255.0.0
17	32768	255.255.128.0
18	16384	255.255.192.0
19	8192	255.255.224.0
20	4096	255.255.240.0
21	2048	255.255.248.0
22	1024	255.255.252.0
23	512	255.255.254.0
24	256	255.255.255.0
25	128	255.255.255.128
26	64	255.255.255.192
27	32	255.255.255.224
28	16	255.255.255.240
29	8	255.255.255.248
30	4	255.255.255.252
31	2	255.255.255.254
32	1	255.255.255.255

Last edited by nbritton; 10-29-2014 at 12:53 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Understanding CIDR notation for iptables Madone_SL_5.5 Linux - Networking 5 12-07-2007 10:26 AM
IP calculator Blackmeth Slackware 7 08-13-2007 11:31 AM
C Calculator ChrisRain Programming 13 03-27-2007 02:33 AM
programming a calculator with bash xiekke Programming 4 03-18-2006 11:10 PM
A Calculator......... 320mb Programming 0 08-13-2004 04:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration