Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rtt_getter.sh for OpenBSD 6.x #66

Closed
happyhater opened this issue Feb 28, 2021 · 3 comments
Closed

rtt_getter.sh for OpenBSD 6.x #66

happyhater opened this issue Feb 28, 2021 · 3 comments

Comments

@happyhater
Copy link
Contributor

happyhater commented Feb 28, 2021

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 happyhater changed the title rtt_getter.sh for OpenBSD 8.x rtt_getter.sh for OpenBSD 6.x Feb 28, 2021
@happyhater
Copy link
Contributor Author

happyhater commented Mar 2, 2021

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
Copy link
Owner

pierky commented Mar 7, 2021

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
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants