Sponsored Content
Top Forums Shell Programming and Scripting Bash cript to calculate summarize address Post 302965696 by RudiC on Tuesday 2nd of February 2016 07:05:46 AM
Old 02-02-2016
I came up with this - not thoroughly tested, and not necessarily most efficient, and possibly won't run with sh, but seems to work (with e.g. recent bash):
Code:
MAX=0
OLDIP=0
while read LINE
  do    IP=$((0x$(printf "%02X" ${LINE//./ })));
        if [ "$OLDIP" -gt 0 ]
          then  TMP=$((IP ^ OLDIP ))
                if [ "$TMP" -gt "$MAX" ]
                  then MAX=$TMP
                  fi
          fi
        OLDIP=$IP
  done < $1

for ((EXP=1; EXP<=32 && 2**EXP<=MAX; EXP++)); do :; done
NET=$((IP & (2**EXP - 1 ^ 2**32 - 1) ))
printf "SuperNet: %d.%d.%d.%d/%d\n" $((NET>>24)) $((NET>>16&255)) $((NET>>8&255)) $((NET&255)) $((32-EXP));

Put into a script file, make it executable, put your IPs into another file, and run script like
Code:
./script filr
SuperNet: 192.168.1.0/24


Last edited by RudiC; 02-02-2016 at 08:13 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

check if argument is an ip address in bash/sh

Hi all, Can you please suggest a few lines of if statement to check if a variable is an ip address purely in bash/sh? Thanks, Marc (3 Replies)
Discussion started by: marcpascual
3 Replies

2. Shell Programming and Scripting

sort and summarize

Hi Guys, I have a file in UNIX with duplicates, I have use sort command as below to delete duplicates based on the KEY positions/columns but now I do not want to "delete" duplicates but summarize by KEY numeric columns. REALLY NEED HELP... URGENT!!! Thanks in advance. sort -k 1.1,1.92... (6 Replies)
Discussion started by: shotronix
6 Replies

3. UNIX for Dummies Questions & Answers

Project with somewhat simple bourne shell cript..

Right now if I have a file that has the following information (with : as the delimiter) name :address :phone number I need to be able to do the following commands: -n search for a name -p search for a phone number -l search for a last name starting with a particular -c find an... (17 Replies)
Discussion started by: Generic
17 Replies

4. Shell Programming and Scripting

how to calculate netwrk from IP address and netmask using Bitwise AND in shell script

Hi, I am having two variables IP="10.150.12.1" netmask="255.255.255.0" To get network number, I know that a bitwise & will help. networkno=IP & netmask My code is #!/usr/bin/ksh ip="10.150.12.1" netmask="255.255.255.0" networkno="$ip" & "$netmask" echo $networkno I am... (7 Replies)
Discussion started by: chaitanyapn
7 Replies

5. Shell Programming and Scripting

Xapture Plsql Error from Unix Shell cript

Hi Friends, Need your help . I have a shell script which executes the plsql procedure proc_p1. I want to capture the error message when the procuder throws some error message. I codeed in the following way . But it shows Job Success. Kindly anyone give some better idea to over come this ... (2 Replies)
Discussion started by: imipsita.rath
2 Replies

6. UNIX for Dummies Questions & Answers

Calculate the day after bash

Hello I would like to help me create a script that: Given a date, more precisely a year, one month and one day. must calculate the day after -Input: Year, Month, Day -Output: Next day the Year-Month-Day thanks (9 Replies)
Discussion started by: karkov
9 Replies

7. Shell Programming and Scripting

Calculate the number of days between 2 dates - bash script

I wrote the day calculator also in bash. I would like to now, that is it good so? #!/bin/bash datum1=`date -d "1991/1/1" "+%s"` datum2=`date "+%s"` diff=$(($datum2-$datum1)) days=$(($diff/(60*60*24))) echo $days Thanks in advance for your help! (3 Replies)
Discussion started by: kovacsakos
3 Replies

8. UNIX for Dummies Questions & Answers

[Solved] How to calculate in sun bash

I have two problems, and it would be great if someone could help me: The first line does not calculate. I have checked the origin term to calculate the variables and the result is OK. Normal substactions with $xx -100 work, but not in this constallation. I tried it with "| bc" and no result... (2 Replies)
Discussion started by: Pieter0815
2 Replies

9. Shell Programming and Scripting

IP Address LookUp Bash Script

I am new to bash scripting. I want write a script that reads from the first argument file and run nslookup, then prints out each nslookup. Something like below: File name = ip 8.8.8.8 8.8.4.4 Bash shell script: nslookup.sh #!/bin/bash for i in $1 do nslookup $i done I... (7 Replies)
Discussion started by: boldnbeautiful
7 Replies

10. Shell Programming and Scripting

Bash to calculate average of all files in directory and output by part of filename

I am trying to use awk to calculate the average of all lines in $2 for every file in a directory. The below bash seems to do that, but I cannot figure out how to capture the string before the _ as the output file name and have it be tab-delimeted. Thank you :). Filenames in... (3 Replies)
Discussion started by: cmccabe
3 Replies
RIG(6)								   Games Manual 							    RIG(6)

NAME
RIG - Random Identity Generator USAGE
rig [ -f | -m ] [ -d datadir ] [ -c num ] DESCRIPTION
Rig is a utility that will piece together a random first name, last name, street number and address, along with a geographically consistant (ie, they all match the same area) city, state, ZIP code, and area code. It is suitable for such applications as feeding the NY times registration page to fend off junk (snail) mail and telemarketers, or for reg- istering on BBS's to which you don't wish to reveal your real information. OPTIONS
-f, -m Respectively, use female and male names. If neither or both options are selected, RIG uses both female and male names. -d datadir Use data files found in datadir. Without this option, the default directory of /usr/share/rig is assumed. -c num Output num identities. Default is 1. FILES
/usr/share/rig/locdata.idx /usr/share/rig/street.idx /usr/share/rig/fnames.idx /usr/share/rig/mnames.idx /usr/share/rig/lnames.idx HISTORY
Based on "fake" written for MS-DOS. Unfortunately the author and publish date are unknown. First UNIX version written from scratch in 1999. This manpage based on RIG version 1.10. AUTHORS
UNIX version (c) 1999-2002 Ian Turner with contributions from Ian Prowell, Nick Rusnov, and Schneelocke. Geography Database is from the original version and I believe it to be held in the public domain. Names database is from the US Census at http://www.census.gov/genealogy/names/ , courtesy of Trent Stanley. Send all comments and bug-reports to: vectro@RIG.sourceforge.net 4 JUN 2002 RIG(6)
All times are GMT -4. The time now is 08:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy