Skip to content

Instantly share code, notes, and snippets.

@reyjrar
Created July 5, 2011 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reyjrar/1065582 to your computer and use it in GitHub Desktop.
Save reyjrar/1065582 to your computer and use it in GitHub Desktop.
http_ping
#!/usr/bin/perl
#
# HTTP "PING" Program.
# Allows someone to check latency to a webserver.
#
# Code by Brad Lhotsky <brad@divisionbyzero.net>
# And Mark Thomas <mark@ackers.net>
#
# Distributed under the same license as perl itself.
#
$|++; # autoflush STDOUT
use strict; # _ALWAYS_
use Time::HiRes qw(gettimeofday); # For time calcs
use Getopt::Std; # for command line args.
use Socket; # So we can play with sockets,
# careful, they bite.
my $MAX_REQ_PER_SESSION = 15; # we don't want them requesting forever.
my $MAXLEN = 255; # Max line length.
my $port = 80;
my $DEBUG =0;
my $DBFILE = "debug.out";
my $IMAGES = 0;
my %args = ();
getopts("hiqdc:", \%args);
&help() if $args{h};
$DEBUG = 1 if $args{d};
$MAX_REQ_PER_SESSION = $args{c} || $MAX_REQ_PER_SESSION;
$IMAGES = 1 if $args{i};
my $URL = $ARGV[0];
die "usage: $0 [-iqdhc] http://host.domain.com/page_that_you_want.html\n"
unless $URL =~ m%http://([^/]+)(/(.*))?$%; #/
my $host = $1;
my $pagereq = $2 || "/";
my $page = $3 || "/";
my @CONTIMES = ();
my @REQTIMES = ();
my @TOTALS = ();
my %image_exists;
my $i=0; # Global Counter.
$SIG{INT} = \&end; # trap ctrl+c
if($DEBUG) {
open(DEBUG, ">$DBFILE") or $DEBUG--;
if($DEBUG) {
select DEBUG;
$|++;
select('stdout');
}
}
if ($host =~ m/(.*?):(\d+)$/) { $host = $1; $port = $2; }
print "Requesting $page from $host.\n";
print "=-" x 40, "\n";
for($i=0; $i < $MAX_REQ_PER_SESSION; $i++) {
%image_exists = ();
my $start = gettimeofday;
print "connection ";
local *HTTP;
socket(HTTP,AF_INET,SOCK_STREAM,getprotobyname('tcp'));
if(connect(HTTP,sockaddr_in(80,inet_aton($host)))) {
select(HTTP); $|++; select('stdout');
my $connected = gettimeofday;
my $connect = sprintf("%.3f", ($connected - $start)*100);
print "$connect ms, ";
print "page request ";
print "with images " if $IMAGES;
my $REQUEST = "GET $pagereq HTTP/1.0\r\n";
if(!$args{q}) {
$REQUEST .= 'User-Agent: GetFast/0.1 (Some OS)' . "\r\n" .
"Connection: Keep-Alive\r\n" .
"Host: $host\r\n" .
'Accept: */*' .
"\r\nAccept-Language: en\r\n" .
'Accept-Charset: iso-8859-1,*,utf-8' . "\r\n";
}
$REQUEST .= "\r\n";
if($DEBUG) {
print DEBUG $REQUEST;
}
send(HTTP, $REQUEST, '');
my $line="";
while (<HTTP>) {
if ($_ =~ m/(src).*=(.*?\.(jpg|png|gif)).*$/i ) {
my $therealdeal = $2;
$therealdeal =~ s/\"//g;
getimage($therealdeal,$host,$pagereq) if $IMAGES;
}
if($DEBUG) {
print DEBUG $line;
}
}
my $done = gettimeofday;
my $recvd = sprintf("%.3f", ($done - $connected)*100);
my $total = sprintf("%.3f", ($done - $start)*100);
print "$recvd ms, total $total ms\n";
push @CONTIMES,$connect;
push @REQTIMES,$recvd;
push @TOTALS, $total;
close HTTP;
} else {
print "FAILED!\n";
next;
}
}
&end;
################
# This sub routine gets images
# from a page.
sub getimage {
my ($imgtwo,$thingy,$host,$img_wo_imgtype,$imgtype,$img);
my ($image,$http_host,$directory) = @_;
if ($image =~ m%[h|H][t|T][t|T][p|P]://([^/]+)(/.*)\.(.*)?$%) {
$host = $1;
$img_wo_imgtype = $2;
$imgtype = $3;
$img = "$img_wo_imgtype.$imgtype";
}
elsif ($image =~ m%^(/.*)\.(.*)$%) {
$host = $http_host;
$img_wo_imgtype = $1;
$imgtype = $2;
$img = "$img_wo_imgtype.$imgtype";
}
elsif ($image !=~ m%/%g ) {
$image =~ m%(.*)\.(.*)$%;
#$image_wo_imgtype;
$host = $http_host;
$imgtype = $2;
$thingy = $1;
$directory =~ s/(.*\/)(.*?)$/$1/;
$img = "$directory/$thingy.$imgtype";
}
else {
return;
}
if(exists($image_exists{$img})) {
return;
}
else {
$imgtwo = $img;
$imgtwo =~ s/\//_/g;
$image_exists{$img} = 1;
socket(HTTPIMG,AF_INET,SOCK_STREAM,getprotobyname('tcp'));
if(connect(HTTPIMG,sockaddr_in($port,inet_aton($host)))) {
select(HTTPIMG);
$|++;
select('stdout');
my $IMGREQUEST = "GET $img HTTP/1.0\nHost: $host:80\nAccept: */*\n\n";
send(HTTPIMG, $IMGREQUEST, '');
while(<HTTPIMG>) {
# wait
}
close HTTPIMG;
}
}
}
###############
# This subroutine will display what we've done so far.
sub end {
$SIG{INT} = \&end;
my $CTOTAL = 0;
my $RTOTAL = 0;
my $CAVG = 0;
my $RAVG = 0;
my $CMIN = 0;
my $CMAX = 0;
my $RMIN = 0;
my $RMAX = 0;
my $MAX = 0;
my $MIN = 0;
my $AVG = 0;
my $TOTAL = 0;
my @S_CON = sort { $a <=> $b } @CONTIMES;
my @S_REQ = sort { $a <=> $b } @REQTIMES;
my @S_TOT = sort { $a <=> $b } @TOTALS;
$CMIN = $S_CON[$[]; $CMAX = $S_CON[$#S_CON];
$RMIN = $S_REQ[$[]; $RMAX = $S_REQ[$#S_REQ];
$MIN = $S_TOT[$[]; $MAX = $S_TOT[$#S_TOT];
for(@CONTIMES) { $CTOTAL += $_; }
for(@REQTIMES) { $RTOTAL += $_; }
for(@TOTALS) { $TOTAL += $_; }
$CAVG = sprintf("%.3f", ($CTOTAL/scalar(@S_CON)));
$RAVG = sprintf("%.3f", ($RTOTAL/scalar(@S_REQ)));
$AVG = sprintf("%.3f", ($TOTAL/scalar(@TOTALS)));
print "\n--- $URL statistics ---\n";
print "$i requests sent, ", scalar(@TOTALS), " pages received.\n";
print "connection min/avg/max = $CMIN/$CAVG/$CMAX ms\n";
print "page request min/avg/max = $RMIN/$RAVG/$RMAX ms\n";
print "total times min/avg/max = $MIN/$AVG/$MAX ms\n";
exit;
}
##############################
# Display a help file.
sub help {
print <<" EOF";
http_ping v 0.1
- Requires Time::HiRes to run
- Code by Brad Lhotsky <brad\@divisionbyzero,net>
and Mark Thomas <mark\@ackers.net>
http_ping [-iqh] [-c count] http://url.com/page.html
options:
i - retrieve background/foreground images
from page.
c <count> - number of times to ping site,
default is $MAX_REQ_PER_SESSION
q - quick headers (only sends minimal header
information to server)
h - display this message
EOF
exit(0);
}
@reyjrar
Copy link
Author

reyjrar commented Jul 5, 2011

found this on my server.. wow.

@errzey
Copy link

errzey commented Jul 6, 2011

we were so freaking stupid. you realize this is over 10 years old?

@reyjrar
Copy link
Author

reyjrar commented Jul 6, 2011

I know.. I found some of the code from CTDB too..

@errzey
Copy link

errzey commented Jul 7, 2011

holy fuckballs...CTDB? that was like...my first program ever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment