LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-16-2017, 12:31 PM   #1
blason
Member
 
Registered: Feb 2016
Posts: 122

Rep: Reputation: Disabled
bash script help plz


Hi Folks,

I am trying to write a script where I have Azure networks which are aroung 7000 in numbers and I want to convert those into specific format. Can someone please guide me on bash script?

Lets say I have 192.168.1.0/24

I want that to be converted as 192.168.1.0 255.255.255.0. This part I have done with ipcalc -mn utility but being 7000 in numbers I want to be sure that subnet mask does not miss and gets converted perfectly then I will use the value in a script

let say I have

192.168.1.0/24
192.168.2.0/24
10.10.10.0/22
172.16.3.0/16

So what I have done till now is

cat aws | while read line;do ipcalc -nm $line | tr "=" " " | awk '{print $NF}';done

Which gets converted perfectly now I need that network and subne mask value to go in variable and then can be used as script for CheckPoint firewall to create the network object.

Like this
*************************
create network internal_net
modify network_objects n_192.168.1.0_24bit ipaddr 192.168.1.0
modify network_objects n_192.168.1.0_24bit netmask 255.255.255.0
update network_objects n_192.168.1.0_24bit
*************************

Can someone please help?
 
Old 02-16-2017, 02:24 PM   #2
TheMadIndian
Member
 
Registered: Dec 2007
Distribution: Fedora Slackware CentOS slax RHEL
Posts: 117

Rep: Reputation: 23
If you're looking to store the output you can try this, I'm sure there is a better way but I can't think of one right now

On the cmdline I tested this and it produces the expected result

Code:
outputvar=$(ipcalc -nm 192.168.1.0/24 |tr "=" " " |awk '{print $NF}'); nmsk=$(echo $outputvar | awk '{print $1}'); ipadd=$(echo $outputvar | awk '{print $2}'); echo $nmsk; echo $ipadd
255.255.255.0
192.168.1.0
You could tie it into your script like this but do something other than echo the variables
Code:
cat aws | while read line;do outputvar=$(ipcalc -nm $line |tr "=" " " |awk '{print $NF}'); nmsk=$(echo $outputvar | awk '{print $1}'); ipadd=$(echo $outputvar | awk '{print $2}'); echo $nmsk; echo $ipadd;done
 
Old 02-16-2017, 02:30 PM   #3
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
I don't have the "m" option on the ipcalc for the distro I'm using, but perhaps you can make use of something like this:

ipcalc_data.txt
Code:
192.168.1.0/24
192.168.2.0/24
10.10.10.0/22
172.16.3.0/16
ipcalc_to_azure.gawk
Code:
/^(Address):/   {
                    ip_address = $2 ;
                }

/^(Netmask):/   {
                    net_mask = $2 ; 
                    bit_count = $4 ;
                    
                    
                    print "\ncreate network internal_net"
                    print "modify network_objects n_"  ip_address  "_"  bit_count  "bit ipaddr "  ip_address  ;
                    print "modify network_objects n_"  ip_address  "_"  bit_count  "bit netmask "  net_mask  ;
                    print "update network_objects n_"  ip_address  "_"  bit_count  "bit"  ;
                }

END {  print "" ;  }
ipcalc_to_azure.bash:
Code:
#!/bin/bash

while read  line
    do
        ipcalc -n  "$line"  |  gawk -f ipcalc_to_azure.gawk
    done  <  ipcalc_data.txt
When I run the BASH script I wrote, it produces this output:
Code:
create network internal_net
modify network_objects n_192.168.1.0_24bit ipaddr 192.168.1.0
modify network_objects n_192.168.1.0_24bit netmask 255.255.255.0
update network_objects n_192.168.1.0_24bit


create network internal_net
modify network_objects n_192.168.2.0_24bit ipaddr 192.168.2.0
modify network_objects n_192.168.2.0_24bit netmask 255.255.255.0
update network_objects n_192.168.2.0_24bit


create network internal_net
modify network_objects n_10.10.10.0_22bit ipaddr 10.10.10.0
modify network_objects n_10.10.10.0_22bit netmask 255.255.252.0
update network_objects n_10.10.10.0_22bit


create network internal_net
modify network_objects n_172.16.3.0_16bit ipaddr 172.16.3.0
modify network_objects n_172.16.3.0_16bit netmask 255.255.0.0
update network_objects n_172.16.3.0_16bit
 
Old 02-16-2017, 09:49 PM   #4
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
Hey Guys,

Somehow @rigor sript didnt work but @MadIndian worked for me wih few more tricks.

Thanks a lot guys for your help
 
Old 02-17-2017, 08:07 AM   #5
TheMadIndian
Member
 
Registered: Dec 2007
Distribution: Fedora Slackware CentOS slax RHEL
Posts: 117

Rep: Reputation: 23
Quote:
Originally Posted by blason View Post
Hey Guys,

Somehow @rigor sript didnt work but @MadIndian worked for me wih few more tricks.

Thanks a lot guys for your help
Don't forget to click yes it was helpful then!

Glad I could help
 
Old 02-18-2017, 02:35 PM   #6
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Just in case it might help someone else in the future, for reference I've added the output of the version of the ipcalc command I was using, when invoked with the "--help" option, and then an example of the format of the output when using the "-n" option with a specific IP address and range "bitmask" ( CIDR ) form of the netmask, which should illustrate how I got the results I did.

Output of "ipcalc --help"
Code:
IP Calculator 0.41

Enter your netmask(s) in CIDR notation (/25) or dotted decimals (255.255.255.0).
Inverse netmasks are recognized. If you omit the netmask, ipcalc uses the default
netmask for the class of your network.

Look at the space between the bits of the addresses: The bits before it are 
the network part of the address, the bits after it are the host part. You can
see two simple facts: In a network address all host bits are zero, in a 
broadcast address they are all set. 

The class of your network is determined by its first bits. 

If your network is a private internet according to RFC 1918 this is remarked. 
When displaying subnets the new bits in the network part of the netmask are 
marked in a different color. 

The wildcard is the inverse netmask as used for access control lists in Cisco 
routers. You can also enter netmasks in wildcard notation. 

Do you want to split your network into subnets? Enter the address and netmask 
of your original network and play with the second netmask until the result 
matches your needs. 


Questions? Comments? Drop me a mail... 
krischan at jodies.de
http://jodies.de/ipcalc

Thanks for your nice ideas and help to make this tool more useful: 

Bartosz Fenski
Denis A. Hainsworth
Foxfair Hu
Frank Quotschalla
Hermann J. Beckers
Igor Zozulya
Kevin Ivory
Lars Mueller
Lutz Pressler
Oliver Seufer
Scott Davis
Steve Kent
Sven Anderson
Torgen Foertsch

Usage: ipcalc [options] <ADDRESS>[[/]<NETMASK>] [NETMASK]

ipcalc takes an IP address and netmask and calculates the resulting broadcast, 
network, Cisco wildcard mask, and host range. By giving a second netmask, you 
can design sub- and supernetworks. It is also intended to be a teaching tool 
and presents the results as easy-to-understand binary values. 

 -n --nocolor  Don't display ANSI color codes.
 -b --nobinary Suppress the bitwise output.
 -c --class    Just print bit-count-mask of given address.
 -h --html     Display results as HTML (not finished in this version).
 -v --version  Print Version.
 -s --split n1 n2 n3
               Split into networks of size n1, n2, n3.
 -r --range    Deaggregate address range.
    --help     Longer help text.
 
Examples:

ipcalc 192.168.0.1/24
ipcalc 192.168.0.1/255.255.128.0
ipcalc 192.168.0.1 255.255.128.0 255.255.192.0
ipcalc 192.168.0.1 0.0.63.255


ipcalc <ADDRESS1> - <ADDRESS2>  deaggregate address range

ipcalc <ADDRESS>/<NETMASK> --s a b c
                                split network to subnets
				where a b c fits in.

! New HTML support not yet finished.

ipcalc 0.41
Output of "ipcalc -n 192.168.0.1/24"
Code:
Address:   192.168.0.1          11000000.10101000.00000000. 00000001
Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
Wildcard:  0.0.0.255            00000000.00000000.00000000. 11111111
=>
Network:   192.168.0.0/24       11000000.10101000.00000000. 00000000
HostMin:   192.168.0.1          11000000.10101000.00000000. 00000001
HostMax:   192.168.0.254        11000000.10101000.00000000. 11111110
Broadcast: 192.168.0.255        11000000.10101000.00000000. 11111111
Hosts/Net: 254                   Class C, Private Internet
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
bash: cc command not found PLZ HELP mothkuri Linux - Newbie 6 10-05-2009 04:34 AM
Plz review my Vmware backup script (bash) jbarelds Linux - Server 5 02-06-2009 08:50 AM
Script Hlp PLz ! _ AsiF Linux - Networking 4 11-16-2007 07:26 PM
boot in bash instead of KDE or GNOME PLZ Help speedyhien Mandriva 2 10-21-2003 11:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:06 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