LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-21-2007, 10:18 AM   #16
Jongi
Senior Member
 
Registered: Aug 2003
Distribution: Debian Sid 32/64-bit, F10 32/64-bit
Posts: 1,070

Original Poster
Rep: Reputation: 45

I was referring to the part below of your reply (or were you explaining how the script works )

Quote:
then you just need to replace the CIDR notation with the conventional netmask.
 
Old 06-21-2007, 11:46 AM   #17
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
yeah that's what the 2nd line of the two does.
 
Old 06-23-2007, 12:59 AM   #18
Jongi
Senior Member
 
Registered: Aug 2003
Distribution: Debian Sid 32/64-bit, F10 32/64-bit
Posts: 1,070

Original Poster
Rep: Reputation: 45
When I run the script I get the following error :

Code:
./test: line 10: export: `-m': not a valid identifier
./test: line 10: export: `option:': not a valid identifier
line 10 being the line in my script with export and ipcalc. My debian ipcalc has this as options

Code:
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.
It returns info like this:

Code:
[jongi:~#] ipcalc 216.104.192.0/21
Address:   216.104.192.0        11011000.01101000.11000 000.00000000
Netmask:   255.255.248.0 = 21   11111111.11111111.11111 000.00000000
Wildcard:  0.0.7.255            00000000.00000000.00000 111.11111111
=>
Network:   216.104.192.0/21     11011000.01101000.11000 000.00000000
HostMin:   216.104.192.1        11011000.01101000.11000 000.00000001
HostMax:   216.104.199.254      11011000.01101000.11000 111.11111110
Broadcast: 216.104.199.255      11011000.01101000.11000 111.11111111
Hosts/Net: 2046                  Class C
EDIT: The Debian version installed is 0.41-1

Last edited by Jongi; 06-23-2007 at 01:44 AM.
 
Old 06-23-2007, 01:58 AM   #19
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
[chris@kermit ~]$ ipcalc
ipcalc: ip address expected
Usage: ipcalc [OPTION...]
-b, --broadcast Display calculated broadcast address
-h, --hostname Show hostname determined via DNS
-m, --netmask Display default netmask for IP (class A, B, or C)
-n, --network Display network address
-p, --prefix Display network prefix
-s, --silent Don't ever display error messages

Help options:
-?, --help Show this help message
--usage Display brief usage message


this is under fedora 7, so i assume you could upgrade yours, i doubt the fedora version isn't anythign but the newest.
 
Old 06-23-2007, 02:31 AM   #20
Jongi
Senior Member
 
Registered: Aug 2003
Distribution: Debian Sid 32/64-bit, F10 32/64-bit
Posts: 1,070

Original Poster
Rep: Reputation: 45
I'll be installing FC7 in anycase today. If I can't get a debian version that handles the -m option I will just continue the script under FC.

Chris, can you print what the output of -m looks like?

Last edited by Jongi; 06-23-2007 at 02:36 AM.
 
Old 06-23-2007, 06:05 AM   #21
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
just "NETMASK=255.255.255.0" etc... i.e. *exactly* the right output to be used in a bash script to define a variable called NETMASK
 
Old 06-23-2007, 03:22 PM   #22
Jongi
Senior Member
 
Registered: Aug 2003
Distribution: Debian Sid 32/64-bit, F10 32/64-bit
Posts: 1,070

Original Poster
Rep: Reputation: 45
So I installed FC7. But for various reasons I decide to finish this off in Debian. So I mounted the FC7 install in Debian and copied /bin/ipcalc (FC7) to /bin in Debian. I then ran the script. What I found interesting is that it would in places, insert the system environment variables. As they would appear if you ran env but with export in front of each variable. The script is exactly as you have it here. I will run it in due course in FC to see whether the same happens.

It was not a job to edit it manually, though either it missed one entry or I deleted one by mistake. I am going to go with the latter.
 
Old 06-24-2007, 09:45 AM   #23
Jongi
Senior Member
 
Registered: Aug 2003
Distribution: Debian Sid 32/64-bit, F10 32/64-bit
Posts: 1,070

Original Poster
Rep: Reputation: 45
Chris - Although I still have not solved the minor problem detailed above, I want to give you my heartfelt appreciation for all the effort and help you have put in here. It has helped me enormously.

I will take some time to actually try and work out what everything does and I might come back with some questions. Again, thanks immensely.
 
Old 06-24-2007, 11:49 AM   #24
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
no problem.
 
Old 07-06-2007, 05:19 AM   #25
Jongi
Senior Member
 
Registered: Aug 2003
Distribution: Debian Sid 32/64-bit, F10 32/64-bit
Posts: 1,070

Original Poster
Rep: Reputation: 45
Chris - The problem persists in FC7 ie, that variables that are output when I run export from the command line appear in the output file (with export and a space ahead of them). For example, export HOME=/home/jongi appears in the file.


Below is my interpretation of the two lines:

Code:
  export $(ipcalc -m $(echo $route | sed -e 's/^.*net\ \([0-9\.\/]*\).*$/\1/'))
  echo $route | sed -e "s/\/[0-9]*/ netmask $NETMASK/"
I assume the first line sets up the variable NETMASK.

Code:
echo $route | sed -e 's/^.*net\ \([0-9\.\/]*\).*$/\1/'
each line of newroutes.txt is echoed and that output is processed by sed. Now how the notation for sed is translated is a bit hard for me.

Code:
^.*net\ \([0-9\.\/]*\).*$
This is what sed searches for in the echo output.

and replaces it with

Code:
/\1/
How to interpret it is confusing me. My guess though (based on what it is I am trying to achieve here) is that it outputs something like 216.104.192.0/21. And this is then used by ipcalc to run ipcalc -m 216.104.192.0/21. This returns NETMASK=255.255.248.0 which is then set as the variable NETMASK by export.

Code:
echo $route | sed -e "s/\/[0-9]*/ netmask $NETMASK/"
The second line takes the same line that has been processed above and replaces /21 with netmask 255.255.248.0. Does it as follows:

Code:
echo $route | sed -e "s/\/[0-9]*/ netmask $NETMASK/"
Sed again processes the output of echo. Sed searches for

Code:
/[0-9]*
a number starting with 0-9, the start means it is more than 1 digit (?) and preceded by / ie a number like /21

Code:
/ netmask $NETMASK/"
replaces /21 with netmask 255.255.248.0.

Last edited by Jongi; 07-06-2007 at 05:22 AM.
 
Old 07-06-2007, 02:56 PM   #26
Jongi
Senior Member
 
Registered: Aug 2003
Distribution: Debian Sid 32/64-bit, F10 32/64-bit
Posts: 1,070

Original Poster
Rep: Reputation: 45
Given my limited knowledge I think this is what is causing the problem

Code:
[root:~#] cat newroutes.txt | grep host | more
route add -host 155.237.171.98/32 ppp1
route add -host 163.195.16.25/32 ppp1
route add -host 163.195.16.155/32 ppp1
route add -host 164.147.241.195/32 ppp1
route add -host 196.6.240.2/32 ppp1
route add -host 196.14.176.115/32 ppp1
route add -host 196.23.48.244/32 ppp1
route add -host 196.23.48.245/32 ppp1
route add -host 196.34.16.71/32 ppp1
There also happen to be the same number of instances where a problem occurs. My suspicions lie with this part of the script

Code:
^.*net\ \([0-9\.\/]*\).*$
 
Old 07-06-2007, 03:01 PM   #27
Jongi
Senior Member
 
Registered: Aug 2003
Distribution: Debian Sid 32/64-bit, F10 32/64-bit
Posts: 1,070

Original Poster
Rep: Reputation: 45
I pipe the output of the script in post 13 to a file called netroutes.txt

Code:
[root:~#] cat netroutes.txt | grep host
export HOSTNAME="localhost.localdomain"
export SESSION_MANAGER="local/localhost.localdomain:/tmp/.ICE-unix/23350"
route add -host 155.237.171.98 netmask 255.255.0.0 ppp1
export HOSTNAME="localhost.localdomain"
export SESSION_MANAGER="local/localhost.localdomain:/tmp/.ICE-unix/23350"
route add -host 163.195.16.25 netmask 255.255.0.0 ppp1
export HOSTNAME="localhost.localdomain"
export SESSION_MANAGER="local/localhost.localdomain:/tmp/.ICE-unix/23350"
route add -host 163.195.16.155 netmask 255.255.0.0 ppp1
export HOSTNAME="localhost.localdomain"
export SESSION_MANAGER="local/localhost.localdomain:/tmp/.ICE-unix/23350"
route add -host 164.147.241.195 netmask 255.255.255.0 ppp1
export HOSTNAME="localhost.localdomain"
export SESSION_MANAGER="local/localhost.localdomain:/tmp/.ICE-unix/23350"
route add -host 196.6.240.2 netmask 255.255.255.0 ppp1
export HOSTNAME="localhost.localdomain"
export SESSION_MANAGER="local/localhost.localdomain:/tmp/.ICE-unix/23350"
route add -host 196.14.176.115 netmask 255.255.255.240 ppp1
export HOSTNAME="localhost.localdomain"
export SESSION_MANAGER="local/localhost.localdomain:/tmp/.ICE-unix/23350"
route add -host 196.23.48.244 netmask 255.255.255.240 ppp1
export HOSTNAME="localhost.localdomain"
export SESSION_MANAGER="local/localhost.localdomain:/tmp/.ICE-unix/23350"
route add -host 196.23.48.245 netmask 255.255.255.240 ppp1
export HOSTNAME="localhost.localdomain"
export SESSION_MANAGER="local/localhost.localdomain:/tmp/.ICE-unix/23350"
route add -host 196.34.16.71 netmask 255.255.255.224 ppp1
 
Old 07-08-2007, 12:22 PM   #28
Jongi
Senior Member
 
Registered: Aug 2003
Distribution: Debian Sid 32/64-bit, F10 32/64-bit
Posts: 1,070

Original Poster
Rep: Reputation: 45
The solution is pretty easy actually.

Code:
cat newroutes.txt | while read route
do 
  export $(ipcalc -m $(echo $route | sed -e 's/^.*net\ \([0-9\.\/]*\).*$/\1/'))
  echo $route | sed -e "s/\/[0-9]*/ netmask $NETMASK/"
done > incorrect.txt
cat incorrect.txt | grep route > correct.txt
correct.txt is as i expected it.
 
Old 07-08-2007, 12:37 PM   #29
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
i've lost a bit of the track of this thread TBH, and from the output above, i'm not sure where those variables come from, currently too exhausted to look too!. Not that elegant a solution really though, should need an intermediate file should you?

Code:
cat newroutes.txt | while read route
do 
  export $(ipcalc -m $(echo $route | sed -e 's/^.*net\ \([0-9\.\/]*\).*$/\1/'))
  echo $route | sed -e "s/\/[0-9]*/ netmask $NETMASK/"
done | grep route > correct.txt
or if it's actually the export line giving that additional output...
Code:
cat newroutes.txt | while read route
do 
  export $(ipcalc -m $(echo $route | sed -e 's/^.*net\ \([0-9\.\/]*\).*$/\1/')) > /dev/null
  echo $route | sed -e "s/\/[0-9]*/ netmask $NETMASK/"
done > correct.txt

Last edited by acid_kewpie; 07-08-2007 at 12:40 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
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
Need command to search and replace text in file acascianelli AIX 12 04-11-2007 08:16 PM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 05:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM

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

All times are GMT -5. The time now is 12:16 PM.

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