Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-12-2004, 02:57 AM
|
#1
|
|
LQ Newbie
Registered: Sep 2003
Location: UK
Distribution: Gentoo
Posts: 22
Rep:
|
"how do I extract a number from a text file using shell command?"
Hello shell script programmers!
Im new to all this so go easy on the explanations please.
I want to use Shell commands only (Not perl) to ping a destination and then assign the average value to a variable.
Ive managed to do it using a combination of 'grep' and 'cut' but I run into problems when the result is more than 100.000 ms or less than 10.000ms since the number of digits is different. Make sense?
So I have:
ping -c 3 "$ip">temp1
grep "rtt" temp1 | cut -b31-36>temp2
read average<ukbabi1u2
This assigns the latency to $average. The cut command simply picks the average value from the line:
rtt min/avg/max/mdev = 63.385/78.393/97.608/14.284 ms
BUT if the results are not xx.xxx then it picks the wrong section.
Any better solutions?
|
|
|
|
02-12-2004, 04:20 AM
|
#2
|
|
LQ Newbie
Registered: May 2003
Location: Sweden
Distribution: Debian
Posts: 3
Rep:
|
how about this:
Code:
average=`ping -c 3 "$ip" | grep rtt | cut -d = -f 2 | cut -d / -f 2`
it will ping the other box, grep the line with the numbers, get the numbers to the right of the equal sign, get the second number in the list and put the result in the variable named average.
/ Dennis
|
|
|
|
02-12-2004, 08:41 AM
|
#3
|
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Using cut , you can count the fields ( f5 and f4 ) separated by the delimitator ( -d/ ) so if you have
rtt min/avg/max/mdev = 63.385/78.393/97.608/14.284 ms
cut -d/ -f5 | cut -d/ -f4 should get you to the right place.
Code:
#!/bin/bash
echo
echo ""
echo "Enter hostname or ip address: "
read host
echo
ping -c 3 ${host} | grep --max-count=1 'rtt' |cut -d/ -f5 | cut -d/ -f4 >temp.txt
ip="temp.txt"
cat ${ip} | \
while read avg
do
echo " average is $avg"
echo
echo
done
|
|
|
|
02-12-2004, 08:54 AM
|
#4
|
|
Member
Registered: Jan 2004
Location: Green Bay
Distribution: RedHat 8.0, LFS-5.0
Posts: 100
Rep:
|
This might be a little overboard, but this is what I use to ping my ISP every 10 minutes. We had an argument over connection uptime, and I won. I had proof that their connection was not up. I had to use this to do it. So, here is what I used to as a cron job.
Someone else might be able to clean it up for you, but it does the job.
Code:
#!/bin/env python
import os, string # This imports String and system functions
import time
tempfile = "/tmp/pingfile"
logfile = "/var/log/pingrate.log"
ipaddr ="xxx.xxx.xxx.xxx"
ptime = "50"
date1 = time.localtime()
date2 = string.split(str(date1), ',')
datecent = date2[0][1:5]
for i in range (1,6) :
date2[i] = string.replace(date2[i], ' ', '0')
date2[i] = string.replace(date2[i], ' ', '0')
date2[i] = string.replace(date2[i], ' ', '0')
seconds = date2[5][1:3]
minutes = date2[4][1:3]
hours = date2[3][1:3]
if (date2[5][2:3] == '') :
seconds = date2[5][0:2]
if (date2[4][2:3] == '') :
minutes = date2[4][0:2]
if (date2[3][2:3] == '') :
hours = date2[3][0:2]
fulldate = "%s/%s/%s" % (date2[1], date2[2][1:3], datecent)
fulltime = "%s:%s:%s" % (hours, minutes, seconds)
os.system ("ping %s -w %s | grep tt | grep -v ttl > %s" % (ipaddr, ptime, tempfile)) # This does the ping. I can't deal with the outpute directly. So,
# instead I write it to a file
exist = os.path.isfile ('%s' % (tempfile))
if (exist) : # See the file was created correctly
file = open( tempfile, 'r' )
recs = file.readlines() # stores the file into the varialbe recs
length = len(recs) # gets the number of lines in recs (the file)
if (length > 1) : # If there are at least 2 lines
a = recs[length - 2] # Sets a to the 2nd to last line
b = recs[length - 1]
b2 = string.split(b,'/') # Does same thing to b, but uses '/' as the delimiter
Ave = b2[4]
Max = b2[5]
a2 = string.split(a, ' ') # Makes an array, a2, where the elements are the portions of 'a' between ' '
Sent = a2[0] # Gets the first ele of a2 (number of packets sent)
Received = a2[3]
Percent = a2[5]
Lost = int(Sent) - int(Received) # Computes the number of packets lost. Sent and Received are strings, so they \
RTime = string.split(a2[8], 'm')
message = "%s, %s, ip=%s, Sent=%s, Lost=%s, Per=%s, Avg=%s, Max=%s ms, RTime=%s" % \
(fulldate, fulltime, ipaddr, Sent, Lost, Percent, Ave, Max, RTime[0])
# print message
os.system ("echo %s >> %s" % (message, logfile)) # prints out the message. You can just redirect
# this wherever you want
if (length < 2) :
os.system ("echo %s, %s, ip=%s, FAILED 100 PERCENT >> %s" % (fulldate, fulltime, ipaddr, logfile))
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:11 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|