Ping not showing with date in bash script
I am trying to write a small script that will monitor the network latency using ping.
I need to write to a file and tag each ping entry with a date and time. I need to see the responses in real time and stop the script if the ping time is too long.
I can get the ping results and summary in an undated file, code below
#!/usr/bin/env bash
echo "Enter Dealer number: "
read deal
echo "Enter IP address: "
read ip
touch ./${deal}_pingtest.txt
ping $ip > ./${deal}_pingtest.txt &
tail -f ./${deal}_pingtest.txt
Stdout results
Enter Dealer number:
test
Enter IP address:
8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=4.87 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=5.36 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=8.30 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=58 time=4.48 ms
^C
File results
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=4.87 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=5.36 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=8.30 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=58 time=4.48 ms
--- 8.8.8.8 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 4.488/5.758/8.309/1.506 ms
when i add date to script, file results never show script statistics with timestamp
#!/usr/bin/env bash
echo "Enter Dealer number: "
read deal
echo "Enter IP address: "
read ip
touch ./${deal}_pingtest.txt
ping $ip | while read pong; do echo "$(date +%Y-%m-%d\|%H:%M:%S): $pong"; done > ./${deal}_pingtest.txt &
tail -f ./${deal}_pingtest.txt
Stdout results
Enter Dealer number:
test
Enter IP address:
8.8.8.8
2017-08-04|11:31:29: PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
2017-08-04|11:31:29: 64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=4.71 ms
2017-08-04|11:31:30: 64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=4.53 ms
2017-08-04|11:31:31: 64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=4.85 ms
2017-08-04|11:31:32: 64 bytes from 8.8.8.8: icmp_seq=4 ttl=58 time=5.11 ms
2017-08-04|11:31:33: 64 bytes from 8.8.8.8: icmp_seq=5 ttl=58 time=4.51 ms
^C
file result
2017-08-04|11:31:29: PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
2017-08-04|11:31:29: 64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=4.71 ms
2017-08-04|11:31:30: 64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=4.53 ms
2017-08-04|11:31:31: 64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=4.85 ms
2017-08-04|11:31:32: 64 bytes from 8.8.8.8: icmp_seq=4 ttl=58 time=5.11 ms
2017-08-04|11:31:33: 64 bytes from 8.8.8.8: icmp_seq=5 ttl=58 time=4.51 ms
2017-08-04|11:31:34: 64 bytes from 8.8.8.8: icmp_seq=6 ttl=58 time=4.89 ms
Thanks everyone for any recommendations.
source to share
I am assuming you are using ctrl-C
your keyboard to interrupt this script. You need to code things so that the command ping
breaks off and outputs a summary, but the shell capturing the output ping
survives to grab that summary and pipe it to the output file.
This seems to work for trap
builtin to bash
.
Setting up the original script:
#!/usr/bin/env bash
read -p "Enter Dealer number: " deal
read -p "Enter IP address: " ip
trap INT
ping $ip | while read pong; do echo "$(date +%Y-%m-%d\|%H:%M:%S): $pong"; done > "$deal"_pingtest.txt &
tail -f "$deal"_pingtest.txt
In the SIGNALS section of the man page for bash
: Commands without inline functions executed by bash have signal handlers set to values inherited by the shell from the parent.
The command trap
above means that the shell executing the while loop will not respond to a keyboard interrupt (signal INT
), but the non-builtin command ping
will have its default location reset if it bash
runs, so it will be interrupted by a keyboard signal. ping
then dumps the summary and outputs, and the shell survives to grab all of that output.
You can also structure things to avoid relying on the background / combination tail
:
#!/usr/bin/env bash
read -p 'Enter Dealer number: ' deal
read -p 'Enter IP address: ' ip
trap '' INT
ping "$ip" |
while read pong; do
echo "$(date '+%Y-%m-%d|%H:%M:%S'): $pong"
done | tee "$deal"_pingtest.txt
Note that in both cases, I use the built-in construct read
and its -p to prompt for input.
source to share
ping
will only show statistics if it was killed with SIGINT
or SIGQUIT
(or if the number of peonies determined with help -c count
is reached, but you are not using it). From man ping
:
When the specified number of packets have been sent (and received), or if the program is terminated with SIGINT, a short summary is displayed. Shorter current statistics can be obtained without terminating the process with the SIGQUIT signal.
So, if you want to print statistics, be sure to run ping
like this:
pkill ping -SIGINT
source to share