Click to See Complete Forum and Search --> : ping script?
ee99ee2
08-28-2002, 12:38 AM
My server is a Debian Potato box. Is there a way I can create a script (or two) to run for about 2 days that will ping 2 hosts? I need to have the result of each ping reported to 2 differnet text files (one for each host). All that it should contain is the amount of time it took to ping the host, and each result should be on a different line. I want to do this to be able to import it into OpenOffice and graph it.
Thanks.
<edit>
Upon more thought, I would also need the date and time reported in this file. Could I have it automaticly make a comma-seperated-value (CSV) file with the ping result in the first column, the date in the second, and the time in the third? So it would look like this:
ping;date;time
ping;date;time
ping;date;time
and so on....
<edit>
-ee99ee2
Rüpel
08-28-2002, 03:05 AM
what is the difference between the ping and the amount of time it took to ping the host?
as far as i know it should be possible to use things like sed or awk and a fair amount of regular expressions and shell-things to solve your problem.
ee99ee2
08-28-2002, 12:04 PM
Nothing.
Eample (google.com):
64 bytes from 216.239.51.101: icmp_seq=1 ttl=45 time=43.9 ms
64 bytes from 216.239.51.101: icmp_seq=2 ttl=45 time=42.5 ms
64 bytes from 216.239.51.101: icmp_seq=3 ttl=45 time=42.3 ms
all I would want in the file would be
43.9;date;time
42.5;date;time
42.3;date;time
see? is this posable?
-ee99ee2
bwkaz
08-28-2002, 01:01 PM
Something like this, maybe?
ping <host> | grep "bytes from" | cut -d "=" -f 4 | cut -d " " -f 1 >logfileObviously, you'd have to change it to get the date & time in there. Unfortunately, I'm not quite sure how to do that... You'd probably need an external script or something.
BTW, the grep "bytes from" part is to cut out lines that aren't actually pings. If you're worried about the host not responding, then you might want to change that.
Here's an idea for adding the date:
DATE=$(date <date options>)
TIME=$(date <other options>)
while read i ; do
echo "$i;$DATE;$TIME"
doneI'm not sure on what options to give date to format the date correctly, but once you get that (man date maybe), you can just add a | otherscriptname before the >logfile in the first one-line script.
Does that help at all?
rapskat
08-29-2002, 03:27 PM
Here is a script I wrote that you might find useful.
I know this might be overkill for what you want, but you may be able to use it as a template for what you are trying to accomplish...
I hope this gets over with the formatting intact...
#!/bin/bash
# SCRIPT NAME: pig
# FUNCTION: checks for up nodes in specifed IP Block
# EXPECTED USAGE: pig [ipblock in format XXX.XXX.XXX] [node number to start at] [node number to stop at] [number times to ping] -[a|i|u|d|s]
# PARAMETERS & OPTIONS: -a = Log all nodes checked; -i = get info on up nodes; -u = Log up nodes only; -d = Log down nodes only; -s = run silent; -x = scan up nodes
# RETURNS: none
# DEPENDANCIES: bash, ping, cat, echo, printf, dig
# INPUT FILES: none
# OUTPUT FILES: pig_log
#
# DEVELOPED BY -
# CREATION DATE - 12/08/2001
# REVISION NUMBER - 0.2
# REVISION DATE - 12/9/2001
# DEVELOPMENT ENVIRONMENT - Linux i686 Kernel Build 2.2.16; Red Hat 7.0 (Guinness); BASH Shell Version 2.04
# COMMENTS -
# +---------------------------------------------------------------------------------------------------------------------+
# + BEGIN SCRIPT +
# +---------------------------------------------------------------------------------------------------------------------+
# ============================================ FUNCTION SECTION ================================================== ========
getUID() {
id ${whoami} | sed -e 's/(.*$//' -e 's/^uid=//'
}
# ========================================== END FUNCTION SECTION ================================================== =======
# ======================================= VARIABLE DECLARATION SECTION ================================================== ===
USAGE="USAGE: pig [ipblock in format XXX.XXX.XXX] [start node number] [stop node number] [times to ping] -[a|u|d|s|i(x)]"
if [ $# -lt 5 ]; then
echo "Insufficient parameters..."
echo "$USAGE"
exit 1
fi
STEALTH=
case $5 in
-*a*) LOGOPTS="all" ;;
-*u*) LOGOPTS="up" ;;
-*d*) LOGOPTS="down" ;;
esac
case $5 in
-*s*) STEALTH="yes" ;;
esac
case $5 in
-*i*) ADDINFO="yes" ;;
esac
case $5 in
-*x*) SCANNODE="yes" ;;
esac
# ===================================== END VARIABLE DECLARATION SECTION ================================================== ==
echo "================================================== ======================" > pig_log
echo PIG NETWORK STATUS LOG FOR SESSION `date +%x` INITITIATED AT `date +%X` >> pig_log
echo "================================================== ======================" >> pig_log
if [ -z "$STEALTH" ]; then
echo "Checking Network IP Addresses $1.$2 - $3 with $4 ping requests..." | tee -a pig_log
else
echo "Checking Network IP Addresses $1.$2 - $3 with $4 ping requests..." >> pig_log
fi
if [ -z "$STEALTH" ]; then
echo "__________________________________________________ _____________________" | tee -a pig_log
else
echo "__________________________________________________ _____________________" >> pig_log
fi
NETMSK="$1"
x=$2
while [ $x -le $3 ]
do
NXTHOST="$NETMSK".$x
if [ -z "$STEALTH" ]; then
printf "Checking IP $NXTHOST"
fi
y=1
RIP=0
while [ $y -le $4 ]
do
if [ -z "$STEALTH" ]; then
printf ". "
fi
if ping -c 1 -w 2 $NXTHOST > /dev/null ; then
RIP=`echo "$RIP + 1" | bc`
fi
y=`echo "$y + 1" | bc`
done
if [ $RIP -gt 0 ] && [ -z "$STEALTH" ] ; then
echo "$RIP replies received"
if [ $RIP -eq $4 ] && [ ! -z ADDINFO ]; then
dig $NXTHOST | tee -a pig_log
if [ -n SCANNODE ]; then
nmap -v -sS -P0 -O $NXTHOST | tee -a pig_log
fi
fi
elif [ -z "$STEALTH" ] ; then
echo "no reply"
fi
if [ $LOGOPTS == all ] ; then
if [ $RIP -gt 0 ] ; then
echo "$RIP of $4 replies received from host $NXTHOST" on `date +%x` at `date +%X` >> pig_log
else
echo "no reply received from host $NXTHOST" on `date +%x` at `date +%X` >> pig_log
fi
elif [ $LOGOPTS == up ] && [ $RIP -gt 0 ]; then
echo "$RIP of $4 replies received from host $NXTHOST" on `date +%x` at `date +%X` >> pig_log
elif [ $LOGOPTS == down ] && [ $RIP -eq 0 ]; then
echo "no reply received from host $NXTHOST" on `date +%x` at `date +%X` >> pig_log
fi
x=`echo "$x + 1" | bc`
done
if [ -z "$STEALTH" ]; then
echo "__________________________________________________ _____________________" | tee -a pig_log
else
echo "__________________________________________________ _____________________" >> pig_log
fi
echo PIG SESSION COMPLETED ON `date +%x` AT `date +%X` >> pig_log
cat pig_log
# +------------------------------------------------- END SCRIPT ----------------------------------------------------------+
bwkaz
08-29-2002, 04:04 PM
If you put your script between [code] tags (the closing tag is the same as the opener, except with a slash preceding code -- like HTML, but with [] instead of <>), it keeps all formatting. It also shrinks the font a little, and makes it monospace.
ee99ee2
08-29-2002, 05:59 PM
That pig script would be great, but it's still not quite what i'm looking for. I wrote a script that does the same thing (I think) only it uses nmap.
I'm going to try to mess around with ping, grep, and cut a little bit. I'll post what I come up with, if anything.
-ee99ee2
rapskat
08-30-2002, 07:09 PM
Originally posted by bwkaz
If you put your script between [code] tags (the closing tag is the same as the opener, except with a slash preceding code -- like HTML, but with [] instead of <>), it keeps all formatting. It also shrinks the font a little, and makes it monospace.
Thanks for the tip! :)
I guess that was pretty *****ious for my first post, huh?
rapskat
08-30-2002, 07:11 PM
Originally posted by ee99ee2
That pig script would be great, but it's still not quite what i'm looking for. I wrote a script that does the same thing (I think) only it uses nmap.
I'm going to try to mess around with ping, grep, and cut a little bit. I'll post what I come up with, if anything.
-ee99ee2
That script uses nmap too somewhere in there, just as an option.
If you get that right, share! :D
bwkaz
08-30-2002, 08:54 PM
Originally posted by rapskat
Thanks for the tip! :)
I guess that was pretty *****ious for my first post, huh? Hey, nothing wrong with that.
ee99ee2
09-03-2002, 05:33 PM
Well I've got it to do actualy what I want it to do, but not really... here's the script so far:
#!/bin/sh
ping -c 1 192.168.1.1 | grep "bytes from" | cut -d "=" -f 4 | cut -d " " -f 1 && echo ';' && date +%D && echo ';' && date +%T
Now for someone that's never programming anything in his life, I think that's pretty good (thanks everyone). The only problem is that for example, here's the results:
cmiller@ned:~$ ./pngscrpt
0.6
;
09/03/02
;
12:32:07
Does anyone know how I can get it all to be printed on one line instead of 5? After I get that to work, I'll add a > 'file name' at the end of it and be ready to go. Also, how can I get it to loop over and over until I hit ctrl+c on the command line to stop it?
Thanks.
-ee99ee2
bwkaz
09-03-2002, 09:57 PM
If you put the whole thing between backquotes (or '$(' and ')' ), and assign it to a variable, then echo the variable, I'm pretty sure that'll be what you're looking for.
To make it repeat, enclose the whole script in a while 1 ; do .... done loop:
#!/bin/bash
while 1 ; do
myvariable=$(...ping stuff goes here...)
echo $myvariable >>file
doneIt's a >> redirection because you won't want to overwrite the file with every line, and > does exactly that. ;)
ee99ee2
09-03-2002, 11:29 PM
I did that, and here it is:
#!/bin/bash
while 1 ; do
var0=$(ping -c 1 192.168.1.1 | grep "bytes from" | cut -d "=" -f 4 | cut -d " " -f 1 && echo ';' && date +%D && echo ';' && date +%T)
echo $var0
done
When I run it, I get this:
cmiller@ned:~$ ./pngscrpt
./pngscrpt: 1: command not found
-ee99ee2