RouterOS rest API requests running in series

Hi,

I'm trying to detect if some devices in my network are connected and rensponding to ping.
I've run into a problem, the later requests time out.
This leads me to believe, that the HTTP requests are handled in series and can not execute multiple commands simultaneously.
If I launch multiple terminal sessions and send pings in there, they can run in parallel.

Is this expected behaviour, or bug?

Thanks for reply,
Jan

My test setup:
RouterOS version: 7.20.4
dispatch powershell script, which starts processes, that ping individual devices in parallel by launching python script:

pings.ps1:

$IPs = '192.168.88.16', '192.168.88.18', '192.168.88.25', '192.168.88.12', '192.168.88.11', '192.168.88.44', '192.168.88.17', '192.168.88.19', '192.168.88.20', '192.168.88.26', '192.168.88.14', '192.168.88.5', '192.168.88.100', '192.168.88.101', '192.168.88.102', '192.168.88.103', '192.168.88.191', '192.168.88.105', '192.168.88.106', '192.168.88.21', '192.168.88.24', '192.168.88.13'

foreach($IP in $IPs){
    Start-Process -FilePath "python" -NoNewWindow -ArgumentList ".\dispatchping.py", $IP
}

dispatchping.py:

import requests
from requests.auth import HTTPBasicAuth
import sys

Username = "admin"
Password = "XXXXXXXXXXXXXXXXXX"

addr = sys.argv[1]

print("ping", addr)

URL = f"http://192.168.88.1/rest/ping"
try:
    res = requests.post(
        URL,
        auth=HTTPBasicAuth(Username, Password),
        data = f'{{"address":"{addr}","count":"4"}}',
        timeout=10
    )
except requests.exceptions.Timeout:
    print("ERR_CONN")
    exit(-1)

print(res.json()[0]["packet-loss"])

this outputs

ping 192.168.88.25
ping 192.168.88.16
ping 192.168.88.12
ping 192.168.88.44
ping 192.168.88.19
ping 192.168.88.11
ping 192.168.88.17
ping 192.168.88.5
ping 192.168.88.26
ping 192.168.88.101
ping 192.168.88.14
ping 192.168.88.20
ping 192.168.88.100
ping 192.168.88.191
ping 192.168.88.102
ping 192.168.88.103
ping 192.168.88.21
ping 192.168.88.106
ping 192.168.88.105
ping 192.168.88.13
ping 192.168.88.24
100
100
0
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN
ERR_CONN

if I dispatch with waiting on each request:

$IPs = '192.168.88.16', '192.168.88.18', '192.168.88.25', '192.168.88.12', '192.168.88.11', '192.168.88.44', '192.168.88.17', '192.168.88.19', '192.168.88.20', '192.168.88.26', '192.168.88.14', '192.168.88.5', '192.168.88.100', '192.168.88.101', '192.168.88.102', '192.168.88.103', '192.168.88.191', '192.168.88.105', '192.168.88.106', '192.168.88.21', '192.168.88.24', '192.168.88.13'

foreach($IP in $IPs){
    Start-Process -FilePath "python" -NoNewWindow -ArgumentList ".\dispatchping.py", $IP -Wait
}

every ping works as expected:

ping 192.168.88.16
0
ping 192.168.88.18
100
ping 192.168.88.25
100
ping 192.168.88.12
0
ping 192.168.88.11
100
ping 192.168.88.44
0
ping 192.168.88.17
0
ping 192.168.88.19
100
ping 192.168.88.20
100
ping 192.168.88.26
100
ping 192.168.88.14
100
ping 192.168.88.5
0
ping 192.168.88.100
100
ping 192.168.88.101
0
ping 192.168.88.102
0
ping 192.168.88.103
100
ping 192.168.88.191
0
ping 192.168.88.105
0
ping 192.168.88.106
0
ping 192.168.88.21
100
ping 192.168.88.24
0
ping 192.168.88.13
100

I've noticed similar, I think it pipelines internally... so connection is "hung" until the previous operation is done. I'm not sure if it's global, or tied to the IP.

I tried replicating the problem in bash, which kinda shows that even though 10+ processes background'ed with curl, the last one takes the sum total of all the previous.

But since you have a timeout of 10 seconds, Python timeouts out first, since the operation does take >10 seconds have just a few pings.


ROSUSER=admin
ROSPASSWD=changeme
ROSURL=http://192.168.88.1

# Array of public DNS servers 
IPS=(
    8.8.8.8           # Google
    8.8.4.4           # Google
    1.1.1.1           # Cloudflare
    1.0.0.1           # Cloudflare
    208.67.222.222    # OpenDNS
    208.67.220.220    # OpenDNS
    9.9.9.9           # Quad9
    149.112.112.112   # Quad9
    8.26.56.26        # Comodo Secure DNS
    8.20.247.20       # Comodo Secure DNS
    94.140.14.14      # AdGuard DNS
    94.140.15.15      # AdGuard DNS
    185.228.168.9     # CleanBrowsing
    185.228.169.9     # CleanBrowsing
    64.6.64.6         # Verisign
    64.6.65.6         # Verisign
    84.200.69.80      # DNS.Watch
    84.200.70.40      # DNS.Watch
    156.154.70.1      # Neustar / UltraDNS
    156.154.71.1      # Neustar / UltraDNS
)

for ip in "${IPS[@]}"; do
  {
    /usr/bin/time curl -k -sS -u "$ROSUSER:$ROSPASSWD" \
    -H 'Content-Type: application/json' \
    -X POST '$ROSURL/rest/ping' \
    --data-raw "{\"address\":\"$ip\",\"count\":4}" \
    | jq -r '.[-1] | "\(.host) \(."avg-rtt") \(.sent) \(.received) \(."packet-loss")"'
  } 2>&1 &
done
wait

which outputs, note the time is showing real which clock time it's been running:

source restapitest.sh
[3] 9077
[4] 9078
[5] 9081
[6] 9085
[7] 9088
[8] 9090
[9] 9094
[10] 9100
[11] 9104
[12] 9106
[13] 9110
[14] 9113
[15] 9118
[16] 9122
[17] 9125
[18] 9130
[19] 9134
[20] 9137
[21] 9141
[22] 9144
        3.07 real         0.00 user         0.01 sys
1.1.1.1 11ms961us 4 4 0
[5]    done       
        6.18 real         0.00 user         0.01 sys
8.8.4.4 15ms80us 4 4 0
[4]    done      
        9.25 real         0.00 user         0.01 sys
8.8.8.8 107ms344us 4 4 0
[3]    done      
       12.24 real         0.00 user         0.01 sys
1.0.0.1 16ms993us 4 4 0
[6]    done      
       15.27 real         0.00 user         0.01 sys
208.67.222.222 19ms747us 4 4 0
[7]    done      
       18.28 real         0.00 user         0.01 sys
149.112.112.112 18ms620us 4 4 0
[10]    done    
       21.32 real         0.00 user         0.01 sys
9.9.9.9 18ms820us 4 4 0
[9]    done     
       24.41 real         0.00 user         0.01 sys
208.67.220.220 20ms452us 4 4 0
[8]    done      
       27.41 real         0.00 user         0.01 sys
94.140.14.14 77ms172us 4 4 0
[13]    done    
       30.54 real         0.00 user         0.01 sys
8.20.247.20 17ms136us 4 4 0
[12]    done    
       33.47 real         0.00 user         0.01 sys
8.26.56.26 17ms571us 4 4 0
[11]    done     
       36.58 real         0.00 user         0.01 sys
94.140.15.15 75ms425us 4 4 0
[14]    done    
       39.64 real         0.00 user         0.01 sys
185.228.169.9 21ms37us 4 4 0
[16]    done    
       43.03 real         0.00 user         0.01 sys
185.228.168.9 106ms139us 4 4 0
[15]    done    
       45.97 real         0.00 user         0.01 sys
64.6.64.6 22ms155us 4 4 0
[17]    done       
       49.02 real         0.00 user         0.01 sys
64.6.65.6 45ms554us 4 4 0
[18]    done     
       52.19 real         0.00 user         0.01 sys
84.200.69.80 160ms530us 4 4 0
[19]    done     
       55.25 real         0.00 user         0.01 sys
156.154.70.1 20ms390us 4 4 0
[21]  - done    
       58.38 real         0.00 user         0.01 sys
84.200.70.40 164ms872us 4 4 0
[20]  - done      
       61.44 real         0.00 user         0.01 sys
156.154.71.1 48ms119us 4 4 0
[22]  + done     
1 Like