LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-01-2018, 09:10 PM   #1
Shakespearr
LQ Newbie
 
Registered: Nov 2018
Posts: 4

Rep: Reputation: Disabled
Post Trying to be figure out the total Usable IP Addresses using whatmask and adding multiple IP addresses in a text file with a script


I have a text file with multiple IP Addresses, and I am having a hard time figuring out where, to begin with, the script. I would like to "whatmask" multiple IP addresses and add up all of the "Usable IP Addresses" to find the total number of Usable IP Addresses. I believe I need to use "Grep", and "Cut", but I am just not sure of where to start.

Thank you!
 
Old 11-01-2018, 10:11 PM   #2
Shakespearr
LQ Newbie
 
Registered: Nov 2018
Posts: 4

Original Poster
Rep: Reputation: Disabled
Any help would be appreciated
 
Old 11-02-2018, 04:06 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
1. show us a bit or all of the file
2, show us what you've tried and the o/p, inc any errors.
3. define what you mean by 'whatmask' ???
 
Old 11-02-2018, 04:03 PM   #4
Shakespearr
LQ Newbie
 
Registered: Nov 2018
Posts: 4

Original Poster
Rep: Reputation: Disabled
Here is what my script looks like so far... I am trying to go through a text file to add all the ip addresses up and finding the total number of "Usable IP Addresses" by using the "whatmask" command which gives you the total number of "Usable IP Addresses".

Code:
y = 0
x = grep 'Usable IP Addresses' | cut -d 'Usable IP Addresses' -f 1 
while read line 
do 
       y=$(($y+$x))
done
Click image for larger version

Name:	IP Addresses List .jpg
Views:	39
Size:	125.9 KB
ID:	28905

Above is a picture of all the IP Addresses that I must add up to find the total amount of Usable IP Addresses available.

Last edited by Shakespearr; 11-02-2018 at 04:37 PM.
 
Old 11-02-2018, 04:23 PM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,744

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
You can post the content of your script in [code] tags here.

I don't see any reference to whatmask in your script. Have you read the man page for whatmask to understand how to use it?

You also need to show us your input file, at least in part.
 
Old 11-02-2018, 04:37 PM   #6
Shakespearr
LQ Newbie
 
Registered: Nov 2018
Posts: 4

Original Poster
Rep: Reputation: Disabled
I added the code above and a screenshot of the input file above.

Thank you!
 
Old 11-02-2018, 05:20 PM   #7
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
You need to start learning shell programming from scratch. Perhaps the bash guide in my signature will help (the bash scripting link is broken).

You need to:

1. run a loop reading one kine after the other from the file e.g.
Code:
total=0
while read IPADDRESS 
do 
.... here you process IPADDRESS
done < ipaddress-file
2. In the loop, run whatmask on the IP address and process its ouput
Code:
whatmask $IPADDRESS | grep ^Usable | awk -F: ‘{ print $2 }’
This code gets the line that starts with Usable. awk then prints the second field of that line, where fields are delimited by a colon.

3. Get the number of usable IP addresses from step 2 and add it up. This is complicated by the fact that whatmask uses commas to structure large numbers like 8,192.
Code:
nip=$(whatmask $IPADDRESS | grep ^Usable | awk -F: ‘{ print $2 }’ | sed ‘s/,//‘)
total=$((total+nip))
This gets the output of the pipeline inside $(...) and assigns it to nip.
The sed command removes commas. It could also be done inside the awk command, but I don’t know the required manipulations by heart.
The second line sums it up. Dollar signs are not needed inside the double parentheses.

This is not tested.
 
Old 11-02-2018, 05:28 PM   #8
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
A few comments regarding your code.
  1. Assignments can’t have white space around the equals sign.
  2. To assign command output, use the $(.....) notation or surround the commad with back-quotes.
  3. The cut command won’t work, because you also need to cut periods and the colon. The field is 2, not 1.
  4. Nowhere do I see the whatmask command or the filename.
  5. You need to perform the grep and cut inside the loop, processing each IP address you read, not outside of it.
 
  


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
[SOLVED] New to Linux - Question: How do you add multiple IP addresses to an host file? TheLinuxLady Linux - Newbie 5 10-23-2018 09:28 AM
i need to write a c code to ping IP addresses which are read from a text file vinothtitan Linux - Newbie 5 10-12-2014 11:23 PM
Program addresses from text editor to ELF file. theKbStockpiler Linux - General 2 10-20-2011 02:04 AM
Script to ping multiple IP addresses one by one peddip Linux - Server 14 09-15-2011 11:44 PM
How can I send mails to multiple addresses in a txt file will824 Linux - Software 8 09-14-2006 10:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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