Linux HTTP -> Packet send delay time (ping)

Let's consider an example that will track the ping time when sending a packet to IP address, TCP port or Web site.

Required materials:

  • Computer: Any computer with a Linux operating system
  • Programs:
    • nano — command-line text editor
    • grep — command-line utility for searching plain-text data sets
    • cURL — command-line program for working with HTTP
    • hping3 – free packet generator and analyzer for TCP / IP, distributed by Salvatore Sanfilippo (also known as Antirez)
    • crontab — software utility used to periodically perform tasks at a specific time or interval.

Device adding and setting:

  1. Add a new device under the name of «Ping test» in VizIoT
  2. Let's configure the device parameters. Let's try to ping Google, IP address, IP port:
    1. tpgoogle key of «Milliseconds, ms» type;
    2. tpip key of «Milliseconds, ms» type;
    3. tpport key of «Milliseconds, ms» type;
  3. Copy pass key and access password in «Basic settings» of the device.

Setting up programs.

We will use «cURL» to send data, and we will use «hping3» to get data on the delay time for sending a packet.

Installing cURL and hping3.

  • On RedHat or CentOS:
    sudo yum install hping3 curl
  • On Debian, Ubuntu, or Linux Mint:
    sudo apt-get install hping3 curl

Let's look at the result of the command:

> hping3 -c 1 -S -1 google.com

In response, you will receive something like this:

HPING google.com (ens33 74.125.232.40): icmp mode set, 28 headers + 0 data bytes
len=46 ip=74.125.232.40 ttl=128 id=16442 icmp_seq=0 rtt=15.1 ms

--- google.com hping statistic ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 15.1/15.1/15.1 ms

Algorithm

Save command result:

res_ping_google=$(hping3 -c 1 -S -1 google.com 2>&1)

Check for the presence of the line "1 packets received" as it means that the packet was delivered successfully:

isOk=$(echo $res_ping_google | grep -Eo '1 packets received')

the default delay value will be 9999 ms, if the packet is delivered successfully, then we will pull out the average time from the result:

time_ping_google=9999
if [ "$isOk" == "1 packets received" ]
then
    time_ping_google=$(echo $res_ping_google | grep -Eo 'min/avg/max = [^ ]*' | grep -Eo '\/[^\/a-z]*\/' | grep -Eo '[^\/a-z]*')
fi

We will use CURL to send HTTP GET requests. Example command with a request:

curl --silent "https://VizIoT.com/update?key=______________&pass=______________&tpgoogle=$time_ping_google" > /dev/null

Now let's write a script that will send information about the temperature of the processor and its cores to the server.

Script creation.

Let's create a script using a text editor:

nano /var/viziot_ping.sh

Script content:

key='_____________'
pass='_________________'
#Ping WEB site
res_ping_google=$(hping3 -c 1 -S -1 google.com 2>&1)
#Ping IP adress
res_ping_ip=$(hping3 -c 1 -S -1 8.8.8.8 2>&1)
#Ping TCP port
res_ping_port=$(hping3 -p 8080 -c 1 -S -1 google.com 2>&1)

time_ping_google=9999
time_ping_ip=9999
time_ping_port=9999

isOk=$(echo $res_ping_google | grep -Eo '1 packets received')
if [ "$isOk" == "1 packets received" ]
then
	time_ping_google=$(echo $res_ping_google | grep -Eo 'min/avg/max = [^ ]*' | grep -Eo '\/[^\/a-z]*\/' | grep -Eo '[^\/a-z]*')
fi

isOk=$(echo $res_ping_ip | grep -Eo '1 packets received')
if [ "$isOk" == "1 packets received" ]
then
	time_ping_ip=$(echo $res_ping_ip | grep -Eo 'min/avg/max = [^ ]*' | grep -Eo '\/[^\/a-z]*\/' | grep -Eo '[^\/a-z]*')
fi

isOk=$(echo $res_ping_port | grep -Eo '1 packets received')
if [ "$isOk" == "1 packets received" ]
then
	time_ping_port=$(echo $res_ping_port | grep -Eo 'min/avg/max = [^ ]*' | grep -Eo '\/[^\/a-z]*\/' | grep -Eo '[^\/a-z]*')
fi

#sending request
curl --silent "https://viziot.com/update?key=$key&pass=$pass&tpgoogle=$time_ping_google&tpip=$time_ping_ip&tpport=$time_ping_port" > /dev/null

Give the right to execute

> chmod +x /var/viziot_ping.sh

We'll use the cron daemon to send every minute. Let's start the cron editor

> crontab -e

There must be such lines at the very beginning:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

And at the very end, add a line that will run the script "/var/viziot_ping.sh" every minute:

0-59 * * * * /var/viziot_ping.sh

Adding widgets:

  1. Create a new dashboard under the name «PING testing»
  2. Add widgets «Ping TIME»
    • type: «Chart»;
    • device: «Ping test»;
    • parameter: tpgoogle , tpip , tpport

Now the setup is complete and you have to wait until your device connected to the server.