How to change ip addressing format from CIDR notation to netmask and vice versa?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change ip addressing format from CIDR notation to netmask and vice versa?
# 1  
Old 12-04-2014
How to change ip addressing format from CIDR notation to netmask and vice versa?

Hi all, I would appreciate if someone could share how to convert CIDR notation to netmask and vice versa.
The value below is just an example. it could be different numbers/ip addresses.

Initial Output, let say file1.txt
Quote:
10.0.0.0/8
172.16.1.0/16
192.168.1.0/24
Final Output, let say file2.txt
Quote:
10.0.0.0 255.0.0.0
172.16.1.0 255.255.0.0
192.168.1.0 255.255.255.0
# 2  
Old 12-04-2014
What's your shell? What's your system?

CIDR to netmask is straightforward if tedious -- just binary math. Shells aren't generally that good at it but modern BASH has large enough numbers to handle this.

Code:
#!/bin/bash

while IFS="/" read IP S
do
        M=$(( 0xffffffff ^ ((1 << (32-S)) -1) ))
        echo "$IP netmask $(( (M>>24) & 0xff )).$(( (M>>16) & 0xff )).$(( (M>>8) & 0xff )).$(( M & 0xff ))"
done

If you have an older bash, it may generate negative values due to overflowing its 32-bit integers.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 12-04-2014
Quote:
Originally Posted by Corona688
What's your shell? What's your system?

CIDR to netmask is straightforward if tedious -- just binary math. Shells aren't generally that good at it but modern BASH has large enough numbers to handle this.

Code:
#!/bin/bash

while IFS="/" read IP S
do
        M=$(( 0xffffffff ^ ((1 << (32-S)) -1) ))
        echo "$IP netmask $(( (M>>24) & 0xff )).$(( (M>>16) & 0xff )).$(( (M>>8) & 0xff )).$(( M & 0xff ))"
done

If you have an older bash, it may generate negative values due to overflowing its 32-bit integers.
Awesome! Thanks Corona688 for the script. It's not what I'm looking for, but the script is good.
Here is my actual question, I've rephrased it and created separate topic for this.

How to read file, and replace certain string with another string?
# 4  
Old 12-05-2014
Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Command for Host Name to IP Address or Vice Versa

Hi, In unix or linux is there any command exist to identify Host Name to IP Address or Vice Versa? Thanks in advance (6 Replies)
Discussion started by: nag_sathi
6 Replies

2. UNIX for Dummies Questions & Answers

How to ping from Windows to Solaris 10 and vice versa?

Hi all, I installed Oracle virtual box 4.1.8 on my desktop. I installed Windows 2008 server R2 as one instance and Solaris 10 as another instance. When am trying to ping from Windows to solaris and vice-versa, ping not working. windows IP : 10.1.47.24 Solaris IP : 10.1.47.25 netstat... (2 Replies)
Discussion started by: kjks
2 Replies

3. Shell Programming and Scripting

Converting from Centigrade to Fahrenheit and vice versa.

I need to write a script that will take the input from a file and convert the number from centigrade to fahrenheit and vice versa. This is what I have but it doesn't seem to be correct. Also the data file has 11 numbers inside of it and the output needs to be listed as so: Fahrenheit Temperature... (18 Replies)
Discussion started by: N1ckNak
18 Replies

4. UNIX for Dummies Questions & Answers

Problem with scp from one server to the other (but not vice versa)

Hi I have a system PRIMARY where I can push or pull files to/from STANDBY using scp. I can also ssh without entering a password. On the STANDBY system if I try and use scp or ssh it asks for a password. I checked in ~/.ssh and there was no authorized_keys file on the PRIMARY server. After... (2 Replies)
Discussion started by: mrrossi
2 Replies

5. Ubuntu

What is the advantage of ubuntu over vista and vice versa?

i am thinking of replacing my vista with ubuntu. Questions: 1) what will be the advantages and disadvantages of using ubuntu instead of vista? 2) what will be the setbacks of replacing my vista? 3) how hard is it to cope up with the new OS? what must i learn to utilize ubuntu? (1 Reply)
Discussion started by: Anna Hussie
1 Replies

6. Shell Programming and Scripting

Uppercase to lowercase and vice versa

shell script to convert file names from UPPERCASE to lowercase file names or vice versa in linux anybody please help me out!!!! (5 Replies)
Discussion started by: jacky29
5 Replies

7. UNIX for Dummies Questions & Answers

Appending from a file to a script and vice versa

Hi, I am new to Unix and discovered this example problem online that I believe will help my learning: Run the command's below env >> xx env >> xx env >> xx env >> xx env >> xx You will now have a file called XX with the env redirected into it 5 times Create a script named... (2 Replies)
Discussion started by: Jimmy_c
2 Replies

8. Shell Programming and Scripting

Pdf to text conversion and vice versa

Hi, I have a pdf file. i want to convert it to text file and do some work on it and later want to convert it back to pdf. Can this be done via unix? or Is there a way unix can directly work on PDF file? (2 Replies)
Discussion started by: saltysumi
2 Replies

9. Shell Programming and Scripting

sed help to convert from lowercase to uppercase and vice versa!

Hello, can sed be used to convert all letters of a file from uppercase to lowercase and vice versa?i know tr command can be used but with sed is it possible? i came up with this :- sed 'y///' file1 actually the above command is also not working! Please help me. Thanks in advance :) (6 Replies)
Discussion started by: salman4u
6 Replies

10. Programming

binary to string conversion and vice versa

please let me know that in unix using c programming language we can do binary to string conversion and vice versa using ltoa and atol but how can we do it in c++ programming language. thank you in advance. (3 Replies)
Discussion started by: kinnaree
3 Replies
Login or Register to Ask a Question