#!/usr/bin/perl
use strict;

use Net::SSH::Expect;
use Carp;

my $pssword="psswd";
my $login="smokeping";


my $ssh = Net::SSH::Expect->new (
            host => "192.168.22.251",
            user => $login,
            password=> $pssword,
            raw_pty => 1,
        );


#       my $login_output = $ssh->login();
#       print $login_output;
#        if ($login_output !~ /Welcome/) {
#            die "Login has failed. Login output was $login_output";
#        }


    $ssh->login('Login','Password');
#$ssh->run_ssh() or die "SSH process couldn't start: $!";

    my $line;


    #Authenticate

#      $ssh->waitfor('yes/no');
#     $ssh->waitfor('/(ogin|name|word):.*$/');
#my     $line=$ssh->exec("yes");
#     $ssh->waitfor('word:');
#     $line=$ssh->exec("$pssword");
#     print $line;
#     $ssh->waitfor('/word:.*$/');
#     $line=$ssh->exec("$pssword");
#     print $line;

#     my $who = $ssh->exec("who");
#     print ($who);

        #Do the work
     $ssh->waitfor('/[\@\w\-\.]+[>#][ ]*$/');
     $line=$ssh->exec("terminal length 0");
     print $line;
     $ssh->waitfor('/[\@\w\-\.]+[>#][ ]*$/');
     $line=$ssh->exec("ping");
     print $line;
     $ssh->waitfor('/Protocol \[ip\]: $/');
     $line=$ssh->exec("ip");
     print $line;
     $ssh->waitfor('/Target IP address: $/');
     $line=$ssh->exec("192.168.22.229");
     print $line;
     $ssh->waitfor('/Repeat count \[5\]: $/');
     $line=$ssh->exec("20");
     print $line;
     $ssh->waitfor('/Datagram size \[100\]: $/');
     $line=$ssh->exec("100");
     print $line;
     $ssh->waitfor('/Timeout in seconds \[2\]: $/');
     $line=$ssh->exec("  ");
     print $line;
     $ssh->waitfor('/Extended commands \[n\]: $/');
     $line=$ssh->exec("y");
     print $line;
     $ssh->waitfor('/Source address or interface: $/');
     $line=$ssh->exec("  ");
     print $line;
     $ssh->waitfor('/Type of service \[0\]: $/');
     $line=$ssh->exec("  ");
     print $line;
     $ssh->waitfor('/Set DF bit in IP header\? \[no\]: $/');
     $line=$ssh->exec("no");
     print $line;
     $ssh->waitfor('/Validate reply data\? \[no\]: $/');
     $line=$ssh->exec("no");
     print $line;
     $ssh->waitfor('/Data pattern \[0xABCD\]: $/');
     $line=$ssh->exec("  ");
     print $line;
     $ssh->waitfor('/Loose, Strict, Record, Timestamp, Verbose\[none\]: $/');
     $line=$ssh->exec("V");
     print $line;
     $ssh->waitfor('/Loose, Strict, Record, Timestamp, Verbose\[V\]: $/');
     $line=$ssh->exec(" ");
     print $line;
#     $ssh->waitfor('/Loose, Strict, Record, Timestamp, Verbose\[none\]: $/');
#     $line=$ssh->exec("Verbose");
#    print $line;
     $ssh->waitfor('/Sweep range of sizes.+$/');
my     $out=$ssh->exec(" n");
#     print $out;
#     $ssh->waitfor('/[\@\w\-\.]+[>#][ ]*$/');
#     @output = $ssh->exec("  ");

my    @output=split(/\r\n/g, $out);

     $ssh->waitfor('/[\@\w\-\.]+[>#][ ]*$/');
#     $line=$ssh->exec("quit");
#     print $line;
     $ssh->close;

     my @times = ();
    while (@output) {
        my $outputline = shift @output;
        chomp($outputline);
#        print "start\n"."$outputline\n"."end\n";
#       print OUTF "$outputline\n";
        $outputline =~ /^Reply to request \d+ \((\d+) ms\)/ && push(@times,$1);
        #print OUTF "$outputline => $1\n";
    }
#    @times = map {sprintf "%.10e", $_ / $self->{pingfactor}} sort {$a <=> $b} @times;
#    close(OUTF);
    print @times;

