MOON
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
System: Linux csr818.wilogic.com 2.6.18-419.el5xen #1 SMP Fri Feb 24 22:50:37 UTC 2017 x86_64
User: digitals (531)
PHP: 5.4.45
Disabled: NONE
Upload Files
File: //scripts.20110531.215904.25158/bsdpkgpingtest
#!/usr/bin/perl
# cpanel10 - bsdpkgpingtest                  Copyright(c) 2005-2006 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

BEGIN {
    unshift @INC,'/usr/local/cpanel';
}

use strict;
use IPC::Open3;
use Cpanel::OSSys;

my ($system,$release,$machine) = (Cpanel::OSSys::uname())[0,2,4];

my $cpbsddir = '/root/.cpbsdpkgs/';

$| = 1;

if (! -e $cpbsddir . 'pingtimes') {
    mkdir $cpbsddir, 0700;
    mkdir $cpbsddir . 'pingtimes', 0700;
}

my @HOSTS;
if (open my $mirrors_fh, '<', $cpbsddir . 'mirrors.lst') {
    while(<$mirrors_fh>) {
        chomp;
        my $mirror = $_;
        my $host = "${mirror}.freebsd.org";
        push @HOSTS, $host;
    }
    close $mirrors_fh;
}

my $now = time();
my $child = 0;

my $pinging = 0;

foreach my $host (@HOSTS) {
    next if (((stat($cpbsddir . 'pingtimes/' . $host))[9] + (86400 * 15)) > $now);

    if ($pinging == 0) {
        print "Testing connection speed...(this could take a while)....";
        $pinging = 1;
    }

    if ($child <= 15) {
        if (my $pid = fork()) {
            $child++;
            print '.';
            #master
        } else {
            my $pingtime = pinghost($host);
            if (open my $pingtime_fh, '>', $cpbsddir . 'pingtimes/' . $host) {
                print {$pingtime_fh} $pingtime;
                close $pingtime_fh;
            }
            else {
                warn "Unable to write ${cpbsddir}pingtimes/$host: $!";
            }
            exit;
        }
    } else {
        sleep 1;
    }

    while (waitpid(-1, 1) > 0) {
        $child--;
    }
}

while (waitpid(-1, 0) > 0) {
    $child--;
}

if ($pinging == 1) {
    print "Done\n";
}

sub pinghost {
    my($host) = @_;
    my($pingtime);

    open(WNULL,'>/dev/null');
    open(RNULL,'</dev/null');

    my $pingflag = '-w';
    if ($system =~ /freebsd/i) { $pingflag = '-t'; }

    open3('<&RNULL',\*PING,'>&WNULL','ping',$pingflag,'3','-c','2',$host);
    while(<PING>) {
        if (/[\d\.]+\/([\d\.]+)/) {
            $pingtime = $1;
        }
    }
    close(PING);
    if (!$pingtime ) { $pingtime = 1000; }

    close(RNULL);
    close(WNULL);

    return $pingtime;
}