LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   parse a file to find an ip address (https://www.linuxquestions.org/questions/programming-9/parse-a-file-to-find-an-ip-address-576708/)

gurucg 08-13-2007 04:44 AM

parse a file to find an ip address
 
hi,

i need an efficient shell script program that should parse a file to find all ip address within it and ping those ip to check whether they r reachable r not....

thanks,
gcg...

95se 08-13-2007 05:28 AM

Quote:

Originally Posted by gurucg (Post 2857132)
hi,

i need an efficient shell script program that should parse a file to find all ip address within it and ping those ip to check whether they r reachable r not....

thanks,
gcg...

Very quick and dirty, no bounds checking (that the parts are between 0-255).

Code:

for i in `cat $1 | grep '\b[0-9]\.[0-9]\.[0-9]\.[0-9]\b'`; ping $i; done

gurucg 08-13-2007 05:43 AM

hi ,
its giving an error
./t6: line 3: syntax error near unexpected token `ping'
./t6: line 3: `ping $line'

95se 08-13-2007 05:53 AM

Code:

for i in `cat $1 | grep '\b[0-9]\.[0-9]\.[0-9]\.[0-9]\b'`; do ping $i; done
You may have to kill ping by using C-c yourself too.

colucix 08-13-2007 06:10 AM

gurucg, just an hint: when testing a script put an echo in front of any command inside a loop, as in
Code:

for i in <some expression here>
do
  echo ping $i
done

in this way you can look at the resulting commands before actually execute them. The idea from 95se is good, but I'd use the following egrep command
Code:

egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
this matches 4 consecutive groups of digit separated by dot, with one to three digit each one. With the -o option egrep prints only the string matching the pattern, that is the parsed IP address. :twocents:

/bin/bash 08-13-2007 06:16 AM

95se I couldn't get your regex to work?

This is what I came up with:
$for i in $(grep -Eo "[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}" yourfile);do ping -c2 -w2 $i;done

/bin/bash 08-13-2007 06:23 AM

Another way to do it:
grep -Eo "[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}" yourfile|
while read i;do
ping -c2 -w2 $i
done

gurucg 08-13-2007 07:07 AM

#
# Internet host table
#
127.100.100.1 localhost
129.0.0.1 b1-lr-09 loghost
10.42.76.1 b1-lr-09-e0 # ADD - DO NOT MODIFY
this is the contents of myfile(host1)..... even though i tried above code .... i am getting some errors.. so please help me out from this

gurucg 08-13-2007 07:08 AM

i am making use of solaris

gurucg 08-13-2007 07:11 AM

which book(or websites) do i refer for shell script programming .....

doublejoon 08-13-2007 08:29 AM

http://en.tldp.org/LDP/abs/html/

colucix 08-13-2007 10:29 AM

Indeed, the usage of regexp on solaris is a bit more restricted and grep/egrep has less options than their counterparts on linux. Parsing an host file as the one you posted could be done by simply
Code:

awk '{print $1}' | grep -v \#
or better (if it works)
Code:

awk '{print $1}' | grep ^[0-9]
By the way, both fail if the file you're parsing contains lines other than comments or IP address/hostname pairs.
Regarding a good shell programming guide, I would suggest the "Bash Guide for beginners" http://www.tldp.org/LDP/Bash-Beginne...tml/index.html, but it is based on the Bourne Again Shell and you may feel a little confused if you're working on solaris, since it implements the Bourne Shell /bin/sh. A lot of BASH features are not available in SH.

95se 08-13-2007 04:08 PM

Here. You may need to change the ping command, but hopefully this will work better for you.

Code:

#!/usr/bin/python

import sys, os, re

PING_COMMAND = "ping -c 3 -w 30"

def isalive(ip):
        ret = os.system(PING_COMMAND + " " + ip + " > /dev/null");
        if ret == 0:
                return True
        return False

commentPattern = re.compile('^\s*#')
ipPattern = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')

for f in sys.argv[1:]:
        try:
                input = file(f, 'r')
        except IOError, e:
                print "Can't read from file " + f
                continue

        for line in input:
                if commentPattern.match(line):
                        continue
                matches = ipPattern.finditer(line)
                for match in matches:
                        ip = match.group()
                        if isalive(ip):
                                print ip + " up"
                        else:
                                print ip + " down"

Put it in a file named pinger.py, make it executable, then run it, with the file names as arguments.

gurucg 08-14-2007 12:22 AM

thanks it worked....

ghostdog74 08-14-2007 01:30 AM

Quote:

Originally Posted by colucix (Post 2857450)
Code:

awk '{print $1}' | grep -v \#
or better (if it works)
Code:

awk '{print $1}' | grep ^[0-9]

awk is more than just that.

Code:

awk '!/#/{print $1}' file
Code:

awk '!/^[0-9]/{print}' file


All times are GMT -5. The time now is 03:11 PM.