ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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....
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
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.
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!
#
# 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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.