LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-13-2007, 04:44 AM   #1
gurucg
LQ Newbie
 
Registered: Aug 2007
Posts: 6

Rep: Reputation: 0
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...
 
Old 08-13-2007, 05:28 AM   #2
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Quote:
Originally Posted by gurucg View Post
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
 
Old 08-13-2007, 05:43 AM   #3
gurucg
LQ Newbie
 
Registered: Aug 2007
Posts: 6

Original Poster
Rep: Reputation: 0
hi ,
its giving an error
./t6: line 3: syntax error near unexpected token `ping'
./t6: line 3: `ping $line'
 
Old 08-13-2007, 05:53 AM   #4
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
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.
 
Old 08-13-2007, 06:10 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 08-13-2007, 06:16 AM   #6
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
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

Last edited by /bin/bash; 08-13-2007 at 06:25 AM. Reason: Make myself clearer!
 
Old 08-13-2007, 06:23 AM   #7
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
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
 
Old 08-13-2007, 07:07 AM   #8
gurucg
LQ Newbie
 
Registered: Aug 2007
Posts: 6

Original Poster
Rep: Reputation: 0
#
# 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
 
Old 08-13-2007, 07:08 AM   #9
gurucg
LQ Newbie
 
Registered: Aug 2007
Posts: 6

Original Poster
Rep: Reputation: 0
i am making use of solaris
 
Old 08-13-2007, 07:11 AM   #10
gurucg
LQ Newbie
 
Registered: Aug 2007
Posts: 6

Original Poster
Rep: Reputation: 0
which book(or websites) do i refer for shell script programming .....
 
Old 08-13-2007, 08:29 AM   #11
doublejoon
Member
 
Registered: Oct 2003
Location: King George, VA
Distribution: RHEL/CentOS/Scientific/Fedora, LinuxMint
Posts: 370

Rep: Reputation: 44
http://en.tldp.org/LDP/abs/html/
 
Old 08-13-2007, 10:29 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 08-13-2007, 04:08 PM   #13
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
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.

Last edited by 95se; 08-13-2007 at 04:09 PM.
 
Old 08-14-2007, 12:22 AM   #14
gurucg
LQ Newbie
 
Registered: Aug 2007
Posts: 6

Original Poster
Rep: Reputation: 0
thanks it worked....
 
Old 08-14-2007, 01:30 AM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by colucix View Post
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
 
  


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
a script to parse a file SamuelHenderson Programming 5 03-15-2007 03:23 AM
how to find physical address of kernel virtual address kushneeraj Programming 0 10-20-2006 07:29 PM
How to find an IP address from the MAC address of a remote machine ? jitz Linux - General 3 01-03-2006 07:55 AM
How to find IP address of a machine if I know their MAC Address dysenteryduke Linux - Networking 13 09-12-2005 10:21 AM
parse HTML file and find keywords ? fnd Programming 8 06-09-2004 12:35 PM

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

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