刚刚写的多线程代码遇到问题,大家帮忙看看问题怎么解决
use Net::Telnet;
use threads;
use threads::shared;
use strict;
use File::Basename;
use constant TELNET_TIMEOUT => 60;
use constant PROMPT_PROMPT => '/[\$%#>] {0,1}$/';
#登陆时的用户名和密码
my $user="cattsoft";
my $password="cattsoft";
#本地文件的名称和所在路径
my $fileName="test1.log";
my $filePath="./log/";
my $file=$filePath.$fileName;
open(LOGFILE1,">>$file") or die"打开文件失败\n";
binmode(LOGFILE1);
LOGFILE1->autoflush(1);
#async('getClient',"192.168.168.184");
#async(\&getClient,"192.168.168.22");
my $thread1=threads->new('getClient',"192.168.168.184");
$thread1->eval();
if($@){
print "error:$@\n";
}
#my $thread2= threads->create(\&getClient,"192.168.168.22","23");
sub getClient{
my ($TelnetServer)=@_;
my $TelnetSession=telnetClient($TelnetServer);
if($TelnetSession->login($user,$password)){
print "登陆采集机成功\n";
}else{
print "登陆采集机失败\n";
exit(1);
}
$TelnetSession->cmd("cd /home/cattsoft/");
my $CurDirectory = $TelnetSession->cmd("pwd");#TelnetCommand($TelnetSession,"pwd");
print $CurDirectory."\n";
if($CurDirectory eq "/home/cattsoft"){
print "转到文件所在目录\n";
}else{
print "转到文件所在目录失败\n";
exit(1);
}
my $report=TelnetCommand($TelnetSession,"more 1.txt");
lock &writeFile;
writeFile($report);
}
sub telnetClient{
my ($TelnetServer)=@_;
my $TelnetSession;
do{
$TelnetSession = new Net::Telnet (Host => $TelnetServer,
Port => '23',
Timeout => TELNET_TIMEOUT,
Cmd_remove_mode => 1,
Errmode => "return",
Prompt => PROMPT_PROMPT);
}until($TelnetSession);
return $TelnetSession;
}
sub writeFile{
my $content=@_;
print LOGFILE1 $content."\n";
}
# 通过 TelnetSession 执行命令,结果返回
sub TelnetCommand {
my($TelnetSession,$strCmd,$intTimeOut) = @_;
$TelnetSession->get(Timeout=>0.02);
my $strReport = "";
my @lines;
if ( defined($intTimeOut) and $intTimeOut > 0 ) {
@lines = $TelnetSession->cmd( String => $strCmd,Timeout => $intTimeOut );
} else {
@lines = $TelnetSession->cmd($strCmd);
}
# 如果 超时,因为@lines是空的,可以尝试从 TelnetSession 的 Socket 中将内容读出作为结果返回
if ($TelnetSession->timed_out) {
$strReport = $TelnetSession->get(Timeout=>0.02);
} else {
for (my $i=0;$i<@lines;$i++) {
$strReport.= $lines[$i];
}
# $strReport.= $TelnetSession->last_prompt();
}
return($strReport);
}
报错:Can't locate object method "eval" via package "threads" at D:/work/develop/companyWork/try/app_state_monitor.pl line 27.
A thread exited while 2 threads were running.