20070118 perl telnet Time::HiRes::gettimeofday
www.yippeesoft.com

好像第一次TELNET速度比较慢 检测一下
#!/usr/bin/perl -w
use strict;
use warnings;
use Net::Telnet;
use   Time::HiRes;
my $begin = Time::HiRes::gettimeofday*1000000;
telnet("192.168.1.20", \’?\’);
my $end = Time::HiRes::gettimeofday*1000000;
my $used=($end-$begin)/1000000;

print "\\r\\n". $used;

exit;

sub telnet &leftsign;
#    print "the telnet method\\n";
#    print $_[0];
    my $reset = "error";
    my @ret;
    my $succeed = "succeed";
    my $telnet_status = "fail";

    my $telnet = Net::Telnet->new(Host => $_[0],
                                  Errmode =>"return",
                                  Timeout =>120
                                 );
    $telnet_status = "ok" if defined $telnet ;
    print $telnet_status;
    if($telnet_status =~ "ok")
    &leftsign;
        $telnet->waitfor(\’/User[: ]*$/i\’);
        $telnet->print(\’sf\’);
        $telnet->waitfor(\’/Password[: ]*$/i\’);
        $telnet->print(\’sf\’);
               my @aa = $telnet->cmd($_[1]);
         print @aa;
 &rightsign;
 $telnet->close;
    $reset;
&rightsign;

(1)Net::FTP

#!/usr/bin/perl -w
# file: ftp_recent.pl
# Figure 6.1: Downloading a single file with Net::FTP
use Net::FTP;

use constant HOST => \’ftp.perl.org\’;
use constant DIR => \’/pub/CPAN\’;
use constant FILE => \’RECENT\’;

my $ftp = Net::FTP->new(HOST) or die "Couldn\’t connect: $@\\n";
$ftp->login(\’anonymous\’) or die $ftp->message;
$ftp->cwd(DIR) or die $ftp->message;
$ftp->get(FILE) or die $ftp->message;
$ftp->quit;

warn "File retrieved successfully.\\n";

———————————————————–
(2)Net::Telnet
#!/usr/bin/perl -w
# file:remoteps.pl

use strict;
use Net::Telnet;
use constant HOST => \’phage.cshl.org\’;
use constant USER => \’lstein\’;
use constant PASS => \’xyzzy\’;

my $telnet=Net::Telnet->new(HOST);
$telnet->login(USER,PASS);
my @lines=$telnet->cmd(\’ps -ef\’);
print @lines;

we can also use beanchmark to catch the run time of the code!!

use benchmark;
timestamp1 = new benchmark;
code here
timesamp2 = new benchmark;

time_run = timediff(timestamp2 – timesamp1);

2. put perl builtin time() in end of your perl script:
$end_time = time();
$elapsed_time = $^T – $end_time;  
# $^T just like $start_time=time() put it very beginn of perl script.
print $elapsed_time;

$^T     The time at which the program began running, in seconds since
               the epoch (beginning of 1970).  The values returned by the -M,
               -A, and -C filetests are based on this value.
use   Time::HiRes;

Source Code:.

#!/usr/bin/perl -w
use strict;
use warnings;
use Net::Telnet;

my $host = "10.0.20.2";
my $user = "dio";
my $passwd = "dio";
my $su_passwd = "123456";

my $t = new Net::Telnet (Timeout => 5);
$t->errmode("return");
$t->open($host);
$t->login($user, $passwd);

$t->print("su -") or die "cmd failed: ", $t->lastline;
$t->waitfor(-match => \’/Password: ?$/i\’,
        -errmode => "return")
or die "no password prompt: ", $t->lastline;

$t->print($su_passwd) or die "cmd failed: ", $t->lastline;;
$t->waitfor(-match => $t->prompt,
        -errmode => "return")
or die "login failed: ", $t->lastline;

my @lines = $t->cmd("whoami") or die "cmd failed: ", $t->lastline;
print @lines;

exit;

历史博文

标签:, , ,
六月 22, 2007 at 10:48 上午 by yippee 1,082 次
Category: Dev
Tags: , , ,