Bash + Ping: [:en]Output with Timestamp[:de]Ausgabe mit einem Zeitstempel

[:en]One of the many things bash is so great to work with, is the flexibility to combinate the hardened tools at hand to create your desired output.

For example, I wanted to ping a host frequently (lets say every two minutes), and just add a readable timestamp to the output. Usually I’d use a network monitoring tool like Nagios for this task, but in this case it was just to discover when a host comes back alive and pop it onto my screen.

After a few minutes of putting pieces of bash commands together, I solved it this way:

[bash]ping -i 120 www.google.com | while read x; do echo "$(date +%T) $x"; done[/bash]

Result:

08:10:24 PING www.google.com (73.94.35.76) 56(84) bytes of data.
05:10:24 64 bytes from 73.94.35.76: icmp_seq=1 ttl=57 time=9.82 ms
05:12:24 64 bytes from 73.94.35.76: icmp_seq=2 ttl=57 time=9.25 ms
05:14:24 64 bytes from 73.94.35.76: icmp_seq=3 ttl=57 time=24.4 ms
05:16:24 64 bytes from 73.94.35.76: icmp_seq=4 ttl=57 time=9.81 ms
05:18:24 64 bytes from 73.94.35.76: icmp_seq=5 ttl=57 time=60.4 ms

Mission accomplished!

[:de]Eine der vielen tollen Annehmlichkeiten von bash ist die Flexibilität, verschiedene gute Werkzeuge miteinander zu kombinieren, um die gewünschte Ausgabe zu erreichen.

Vor ein paar Tagen wollte ich einen Host regelmäßig (p.e. alle zwei Minuten) pingen, und dabei einen Zeitstempel mitführen. Normalerweise verwende ich dafür geeignete Netzwerktools für diese Aufgabe wie z.B. Nagios, aber in diesem Fall wollte ich nur auf dem Bildschirm sehen, wann ein Host wieder online geht.

Nach ein paar Minuten löste ich die Aufgabe mit dieser Kombination:

[bash]ping -i 120 www.google.com | while read x; do echo "$(date +%T) $x"; done[/bash]

Resultat:

08:10:24 PING www.google.com (73.94.35.76) 56(84) bytes of data.
05:10:24 64 bytes from 73.94.35.76: icmp_seq=1 ttl=57 time=9.82 ms
05:12:24 64 bytes from 73.94.35.76: icmp_seq=2 ttl=57 time=9.25 ms
05:14:24 64 bytes from 73.94.35.76: icmp_seq=3 ttl=57 time=24.4 ms
05:16:24 64 bytes from 73.94.35.76: icmp_seq=4 ttl=57 time=9.81 ms
05:18:24 64 bytes from 73.94.35.76: icmp_seq=5 ttl=57 time=60.4 ms

Mission erfolgreich!


Beitrag veröffentlicht

in

von

Schlagwörter:

Kommentare

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.