简体   繁体   English

如何在脚本中使用两个CPAN模块

[英]How to use two CPAN modules in a script

Here I'm trying to use two CPAN modules in a script. 在这里,我试图在脚本中使用两个CPAN模块。 Is there a proper way to use other than what I've used in my code? 除了我在代码中使用的内容之外,还有其他正确的方法吗?

#!/usr/bin/perl -w
$host='example/cp,';
$user='usertest';
$pass='kjasdkjd';
$cmd='su -';
$stdin='jkhasdj';
#$cmd='passwd';
use Net::SSH::Perl;
my $ssh = Net::SSH::Perl->new("$host", options => [
"use_pty 1", "interactive true"]);
$ssh->login($user, $pass);
#my($stdout, $stderr, $exit) = $ssh->cmd($cmd,[$stdin]);
#$ssh->shell;
#print ("$stdout\n");
#print ("$stderr\n");
#print ("$exit\n");

use Net::SSH::Expect;
$ssh->send("passwd");
$ssh->waitfor('password:\s*\z', 1) or die "prompt 'password' not found after 1 second";
$ssh->send("frghthhyj");
$ssh->waitfor(':\s*\z', 1) or die "prompt 'New password:' not found";
$ssh->send("redhat");
$ssh->waitfor(':\s*\z', 1) or die "prompt 'Confirm new password:' not found";
$ssh->send("redhat");

I'm getting error "Can't locate object method "send" via package "Net::SSH::Perl::SSH2" though the "send" method is associated with "Net::SSH::Expect" the script looks for the method in "Net::SSH::Perl" which is not correct. 我收到错误"Can't locate object method "send" via package "Net::SSH::Perl::SSH2"虽然”发送“方法与”Net :: SSH :: Expect“相关联,但脚本看起来对于“Net :: SSH :: Perl”中的方法不正确。

Look at the documentation for Net::SSH::Expect . 查看Net::SSH::Expect的文档。 It shows how to create an instance of the object, login, etc. 它显示了如何创建对象的实例,登录等。

You can't login via Net::SSH::Perl and magically convert your instance $ssh into an Expect object. 您无法通过Net::SSH::Perl登录并将您的实例$ssh神奇地转换为Expect对象。 Just start with Expect from the very beginning. 从一开始就从Expect开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM