Ping command s timestamp + response time

Všetko ohľadom Linuxu...
ob3l1x
Moderátor
Moderátor
Používateľov profilový obrázok
Príspevky: 5074
Registrovaný: 03 okt 2004, 20:31
Bydlisko: New Zeland

Ping command s timestamp + response time

Príspevok od používateľa ob3l1x »

Ahojte potreboval by som nejaky script alebo len command na ping
ktory mi ku kazdemu pingu hodi timestamp + viem mu zadat ze nech zapisuje len ping ktory bude mat response time > 1ms.

na time stamp som nasiel nieco taketo co aj funguje :

Kód: Vybrať všetko

ping HOSTNAME | perl -nle 'print scalar(localtime), " ", $_'
alebo

Kód: Vybrať všetko

ping HOSTNAME | awk '/^[0-9]+ bytes from / { "date" | getline pong; close("date"); print pong":",$0; }'
problem je len ten ze to vypisuje vsetky a ja chcem len urcite.

Ak by ste niekto vedeli pomoct dajte vediet dik :)
pato83
Addict
Addict
Používateľov profilový obrázok
Príspevky: 3916
Registrovaný: 21 nov 2005, 22:12

Re: Ping command s timestamp + response time

Príspevok od používateľa pato83 »

Kód: Vybrať všetko

#!/bin/bash

#############################################################################################
# Author: Patrik Uhrak
# Email: [email protected]
# Licence: GPLv3 ; see for more info: http://www.gnu.org/licenses/gpl.html
# Version: 0.1-01122012
# Description: This script takes the output of GNU ping of the command 'ping -D hostname'.
#      It repositions the ping timestamp to the end of each line if the time of ping
#      is lower than the one given as the parameter to this script. If the time of
#      ping is higher than the one given as the parameter, then it is deleted, but the
#      line itself is kept and printed.
#############################################################################################

shopt -s extglob
shopt -s nocasematch
# By default it fails with an error message, because we set deftime to zero.
# So you must set deftime at least to get some resuslts and in that case it gives the timestamp to any response
# lower than the given value.
declare defway='<' deftime=0 human=0
# ping -D gives the timestamp in microsends, therefore 6 repetitions of any number from the given range
declare -r regexp='^(\[[0-9]+.[0-9]{6}\]) [0-9]+ bytes from.*time=([0-9]+.?[0-9]*) ms$'

CheckApp()
{
if ! which "$1" &>/dev/null
then
   echo -e "We need $1 in order to work properly.\nPlease install it first."
   exit 1
fi
}

Help()
{
cat <<EOF
Possible options:
1. higher,lower (default) or equal
2. a number with or without a decimal number
3. h, help, -h, --help - invokes help and exits with the exit status of 0
4. human - timestamp will be converted to human readable format

If any error should occur, then the exit status is 1.

Example: ping -D hostname | "${0##*/}" higher 50
   - keeps the timestamp given by ping if response time is higher than 50
EOF
}

Control()
{
while [[ $# -gt 0 ]]
do
	case "$1" in
		higher|lower|equal)
         	# this is already globally declared
         	case "$1" in
			higher)
				defway='>'
			;;
			lower)
				defway='<'
			;;
			equal)
				defway='=='
			;;
		esac
	;;
	+([0-9])?(.)*([0-9]) )
		# this is already globally declared
		deftime="$1"
      	;;
		help|h|-h|--help )
		Help
		exit 0
 	;;
		human)
		# this is already globally declared
		human=1
	;;
	* )
		echo -e "Unrecognized argument was given.\n"
		Help
		exit 1
	;;
   esac
   shift
done
}

HigherThanZero()
{
if [[ $(echo "$1" '>' 0 | bc ) -eq 0 ]]
then
   echo "You must set a number and it must be higher than zero."
   exit 1
fi
}

Compare()
{
# the following variables are accessible in this function only.
# ptime - ping time
# ttime - timestamp
# data - any data
declare data ptime ttime

while read data
do
   if [[ "$data" =~ $regexp ]]
   then
      # number of seconds since the epoch (1.1.1970) with microseconds to be more exact
      # easier for sorting and easy to convert to more human readable format
      ttime="${BASH_REMATCH[1]}"
      ptime="${BASH_REMATCH[2]}"
      # bc gives 1 if the given expression is true, otherwise it gives 0
      if [[ $( echo "$ptime" "$1" "$2" | bc ) -eq 1 ]]
      then
         ttime="${ttime/[}"
         ttime="${ttime/]}"
         [[ "$3" -eq 1 ]] && ttime="$(date -u --date="@${ttime}" +'%F %R:%S') ${ttime#*.}"
         echo "${BASH_REMATCH[0]/\[*\] } timestamp=${ttime}"
      else
         echo "${BASH_REMATCH[0]/\[*\] }"
      fi
   else
      echo "$data"
   fi
done
}

CheckApp 'bc'
Control "$@"
HigherThanZero "$deftime"
Compare "$defway" "$deftime" "$human"

exit 0
Ak je ziadany cas od 1.1.1970, vysledok vyzera takto:
patrik@thinkpad:~/test$
ping -c 4 -D http://www.google.com | bash ./stamp.bash lower 50
PING http://www.google.com (173.194.78.106) 56(84) bytes of data.
64 bytes from wg-in-f106.1e100.net (173.194.78.106): icmp_req=1 ttl=45 time=42.1 ms timestamp=1354293512.545892
64 bytes from wg-in-f106.1e100.net (173.194.78.106): icmp_req=2 ttl=45 time=37.0 ms timestamp=1354293513.538335
64 bytes from wg-in-f106.1e100.net (173.194.78.106): icmp_req=3 ttl=45 time=104 ms
64 bytes from wg-in-f106.1e100.net (173.194.78.106): icmp_req=4 ttl=45 time=41.2 ms timestamp=1354293515.546248

--- http://www.google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 37.094/56.185/104.227/27.802 ms
Ak je ziadany cas, ktory je cloveku viac blizsi, ale tazsie sa s nim robi, tak nasledovne je vystupom:
patrik@thinkpad:~/test$ ping -c 4 -D http://www.google.com | bash ./stamp.bash lower 50 human
PING http://www.google.com (173.194.78.103) 56(84) bytes of data.
64 bytes from wg-in-f103.1e100.net (173.194.78.103): icmp_req=1 ttl=44 time=42.0 ms timestamp=2012-11-30 16:38:59 201002
64 bytes from wg-in-f103.1e100.net (173.194.78.103): icmp_req=2 ttl=44 time=73.3 ms
64 bytes from wg-in-f103.1e100.net (173.194.78.103): icmp_req=3 ttl=44 time=47.5 ms timestamp=2012-11-30 16:39:01 181847
64 bytes from wg-in-f103.1e100.net (173.194.78.103): icmp_req=4 ttl=44 time=40.9 ms timestamp=2012-11-30 16:39:02 178538

--- http://www.google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 40.942/50.964/73.304/13.139 ms
Dolezite je pouzit prikaz ping s parametrom -D . Ak by si mal daku variaciu ping-u, ktora by nemala dany parameter alebo jeho vystup by bol odlisny od mojho, tak daj vediet. Opravene, ale stale to moze mat dake chybicky. U mna to zatial za roznych okolnosti funguje. Zatial
ob3l1x
Moderátor
Moderátor
Používateľov profilový obrázok
Príspevky: 5074
Registrovaný: 03 okt 2004, 20:31
Bydlisko: New Zeland

Re: Ping command s timestamp + response time

Príspevok od používateľa ob3l1x »

Dakujem za odpoved ale kolega zbuchal mensi scriptik za pomoci awk.
pato83
Addict
Addict
Používateľov profilový obrázok
Príspevky: 3916
Registrovaný: 21 nov 2005, 22:12

Re: Ping command s timestamp + response time

Príspevok od používateľa pato83 »

V poriadku. Velkost tvori najma overovanie vstupov a inych nutnosti. Ak toto vsetko nie je treba, tak si vystacis s 3/4 funkcie Compare a nahradenim hodnot. Ale mas to v awk-u, takze nie je co riesit.

By ma zaujimalo, ako to vyzera v awk-u, ak je to mozne. Dik.
Napísať odpoveď