-1

Looking for some greatly appreciated help writing a bash script on OpenWRT. I want the script to check ping response times for an OpenVPN connection and if the response time in ms goes over a certain threshold to then perform an action such as change the configuration file to connect to a different OpenVPN server. I will set the script up via crontab on the OpenWRT firewall.

So far I have worked out the command to get the ping result in ms that I need for the query.

ping -c 1 1.1.1.1 |  awk 'FNR == 2 { print $(NF-1) }' | cut -d'=' -f2

This will ping the IP address (in this instance Cloudfare DNS) and print the ping ms time to the screen.

I can also modify this command to write the value to a file instead by using stdbuf

ping -c 1 1.1.1.1 |  awk 'FNR == 2 { print $(NF-1) }' | stdbuf -o0 cut -d'=' -f2 > pingms

I need some help on then taking this value either via stdout or output to a text file and performing an action if the value is greater than say 100ms.

eg. if value is greater than 100ms (from command above) then execute additional command / script, otherwise do nothing

Any help would be appreciated.

Thanks

stavster
  • 1
  • 1
  • Could you please edit the post to clarify and narrow down the question? What is something you've already tried, and what is the error you're seeing, for example? – Don Rowe Feb 11 '20 at 00:32

2 Answers2

0

Something like this maybe.

#!/usr/bin/env sh

if [ $(ping -c 1 1.1.1.1 |  awk 'FNR == 2 { print $(NF-1) }' | stdbuf -o0 cut -d'=' -f2 ) -gt 100 ]; then
  echo "greater than 100 do something!"
else
  echo 'less than 100 nothing to do, bye...'
fi
Jetchisel
  • 7,493
  • 2
  • 19
  • 18
0

First of all, on OpenWRT you will not have a BASH shell, rather a shell with "limitations", i.e. ash. Also stdbuf is normally not part of the official builds and busybox, and various shell/architecture combinations are handling floats differently, so I recommend to cut the fraction to keep your script architecture- and build-independent. I hope the following oneliner will help you out.

[ $(ping -c1 1.1.1.1 | awk 'FNR == 2 { print $(NF-1) }' | cut -d'=' -f2 | cut -d'.' -f1) -gt 100 ] && echo "GT 100" || echo "LT 100"
molnarg
  • 445
  • 4
  • 8