pierky / arouteserver

A tool to automatically build (and test) feature-rich configurations for BGP route servers.
https://arouteserver.readthedocs.org/
GNU General Public License v3.0
284 stars 46 forks source link

rtt_getter.sh for OpenBSD 6.x #66

Closed happyhater closed 3 years ago

happyhater commented 3 years ago

Hello,

By default we have ksh instead of bash, and the output from round-trip are different than Linux, so:

Thank you, --z

#!/bin/ksh
peer_ip="$1"

if [ -z "$peer_ip" ]; then
        echo None
        exit
fi

if [ $(echo "$peer_ip" | grep :) ]; then
        ping_ping6="ping6"
else
        ping_ping6="ping"
fi

# if you use '-i 0.2' you'll need super user access
data="`$ping_ping6 -c 3 -i 1.2 -n -q -w 1 $peer_ip 2>&1`" || (>&2 echo -en "None"; exit;)

echo "$data" | grep "0 received" &>/dev/null

avg=`echo "$data" | grep "min/avg/max/" | egrep -o " [0-9\.\/]+ ms" | cut -d '/' -f 2`
echo $avg
happyhater commented 3 years ago

or--

// ZmEu (whitehat.ro) 03022021
//  - RTT getter program for RTT-based actions (ARouteServer)
//
// $ gcc -o rtt_getter rtt_getter.c && ./rtt_getter 185.1.176.1
// 0.481
//
// I am not responsible for any damage.
// For educational purposes only, use at your own responsibility.
//
// Don't hate me, hate the code!#

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>

int cmd(char *ip) {
        char buf[124];
        char *ping;

        if (inet_pton(AF_INET, ip, buf)) {
                ping = "ping";
        } else if (inet_pton(AF_INET6, ip, buf)) {
                ping = "ping6";
        } else {
                ping = '\0';
        }

        if (ip == NULL || ping == NULL) {
                printf("None\n");
                return 0;
        }

        int result;
        FILE *isalive;
        memset(buf, 0, 124);
        sprintf(buf, "%s -c 1 -n -q -w 1 %s | grep 'packets'", ping, ip);
        isalive = popen(buf, "r");
        while (fgets(buf, sizeof(buf), isalive)) {
                if (!strstr(buf, "0 packets")) {
                        // interval lower than =< 1 needs super user access
                        snprintf(buf, sizeof(buf), "%s -c 3 -i 1 -n -q -w 1 %s | grep min/avg/max | egrep -o ' [0-9./]+ ms' | cut -d '/' -f 2", ping, ip);
                        system(buf);
                } else {
                        printf("None\n");
                }
                return 0;
        }
        pclose(isalive);

        return 0;
}

int main (int argc, char **argv) {
        int error;

        if (argc < 2) {
                fprintf(stderr, "[-] Syntax:\n<%s %%i>\t\t- peer IP address\n", argv[0]);
                return 1;
        }

        if (error = cmd(argv[1])) {
                fprintf(stderr, "[-] exec() returned an error: %s\n", strerror(errno));
        }

        return error;
}
ARouteServer 2021-03-02 18:39:57,678 INFO Enricher 'RTTGetter' started
ARouteServer 2021-03-02 18:40:07,787 INFO Enricher 'RTTGetter', 4 tasks left
ARouteServer 2021-03-02 18:40:10,013 INFO Enricher 'RTTGetter' completed successfully after 13 seconds
$ grep -i rtt /etc/bgpd.conf
        # RTT: 2.483 ms (normalized value: 2)
        # RTT: 1.209 ms (normalized value: 1)
        # RTT: 41.403 ms (normalized value: 41)
        # RTT: 40.268 ms (normalized value: 40)
        # RTT: 42.503 ms (normalized value: 43)
        # RTT: 43.711 ms (normalized value: 44)
# do_not_announce_to_peers_with_rtt_lower_than 5 ms
# do_not_announce_to_peers_with_rtt_lower_than 10 ms
# do_not_announce_to_peers_with_rtt_lower_than 15 ms
# do_not_announce_to_peers_with_rtt_lower_than 20 ms
# do_not_announce_to_peers_with_rtt_lower_than 30 ms
# do_not_announce_to_peers_with_rtt_lower_than 50 ms
# do_not_announce_to_peers_with_rtt_lower_than 100 ms
# do_not_announce_to_peers_with_rtt_lower_than 200 ms
# do_not_announce_to_peers_with_rtt_lower_than 500 ms
pierky commented 3 years ago

Hi @happyhater, that's great, thanks for your contribution. Would you like to open a PR and propose the addition of the two files? They could go inside the config.d directory, where the bash version already exists. You may add both the ksh script and the C program. I could make the changes to the doc where the usage of rtt-getter is explained and get everything merged in the next release. WDYT?

happyhater commented 3 years ago

Hi @pierky , sure! I guess it's better to use just C program, don't you think ? There is it: #67