Perl Net::SSH::Expect.pm 包详解

2017-01-01 21:43:59来源:CSDN作者:qq_32345239人点击

第七城市
Net::SSH::Expect.pm 
new():以哈希的方式指定参数:'host'=> 'user' =>'password' =>'port' => 'raw_pty'=> 创建pty
1. login() 返回所有的结果;可以根据返回结果判断是否登陆成功;2.send() 发送命令,命令输出结果会保存在缓存中;3.before() 返回后上一次匹配的结果5.after() 返回剩余的所有数据6.peek(0) 返回当前缓存里面所有没有移除的信息,不会删除数据;参数为等待的时间 , 一般是0;7.eat(string) 可以做清空 输入缓存用; 删除输入缓存中相应的字符串example :eat(peek(0));;8.read_all()读取并移除所有的输出;9.has_line 判断缓存区是否还有数据;如果有返回1 没有返回010.read_line 读取并移除一行输出;11.exec (cmd) 执行一个命令 并返回所有的输出结果;12.get_expect 用来管理ssh链接,如果存在这个链接,就返回这个链接,不存在报错
常用方法演示:
#!/usr/bin/perluse strict;use Net::SSH::Expect;my $ssh ;my $line;eval {	$ssh = new Net::SSH::Expect (		'host' => '127.0.0.1',		'user' =>'root',		'password' => '1qazXSW@',		'port' => 22,		'timeout' =>1,		'raw_pty' => 1);		if ($ssh->login() =~ /Password/i){		print "password  is  wrong !/n";		exit -1;	}};$ssh->send("cat  /home/xing/all_bak/y.txt");$ssh->waitfor ("/$/>/%/#");print "peek : ------------------------------------/n";print $ssh->peek(0) ."/n";print "peek over  --------------------------------/n";print "read_line : -------------------------------/n";while($line = $ssh->read_line()){	print $line."/n";}print "read_line  over --------------------------/n";$ssh->send("cat  /home/xing/all_bak/y.txt");$ssh->waitfor ("/$/>/%/#");print "read_all : -------------------------------/n";print $ssh->read_all() ."/n";print "read_all over ---------------------------/n";$ssh->send("cat  /home/xing/all_bak/y.txt");$ssh->waitfor ("/$/>/%/#");print "eat test: --------------------------------/n";$ssh->eat($ssh->peek(0));print $ssh->peek(0) ."/n";print $ssh->read_all() ."/n";print "eat test over ----------------------------/n";$ssh->send("cat  /home/xing/all_bak/y.txt");$ssh->waitfor ("/$/>/%/#");print "after ; ---------------------------------/n";$ssh->read_line();$ssh->read_line();print $ssh->after() ."/n";print "after over -----------------------------/n";$ssh->close();

运行结果:



第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台