#!/usr/local/bin/perl use IO::Socket; use Getopt::Long; sub GetHumanReadable { # as were specified at wikipedia http://en.wikipedia.org/wiki/Megabyte # 1 MB = 10^6 for networking contexts my $value = shift; my $measure="Bytes"; if ( $value > 1000){ $value = $value/1000; $measure = KB; } if( $value > 1000) { $value = $value/1000; $measure = MB; } if ( $value > 1000) { $value = $value/1000; $measure = TB; } return($value,$measure); } sub PrintHelp { print <<'EOF'; bandtest.pl -h hostname or ip address -p port -f file --help to see this help EOF exit 0; } my ($content,$size,$start_time,$end_time, $duration,$speed,$value,$measure, $size_val, $size_measure, $file, $port, $host,$help); GetOptions ( "-h=s" => \$host, "-p=i" => \$port, "-f=s" => \$file, "--help" => \$help ); PrintHelp if ($help); PrintHelp unless (defined($host) and defined($port) and defined($file)); my $sock = new IO::Socket::INET ( PeerAddr => $host, PeerPort => $port, Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock; open(FILE,"); close(FILE); $start_time = time(); print $sock "$content"; $end_time = time(); $duration = $end_time-$start_time; $speed = $size/$duration if ($duration > 0); ($value,$measure) = GetHumanReadable($speed); ($size_val, $size_measure) = GetHumanReadable($size); printf("Bytes were transfered %.2f %s \nSeconds taken: %d\nAverage speed %.2f %s\n", $size_val, $size_measure, $duration, $value, $measure); close($sock);