简体   繁体   中英

How to use two CPAN modules in a script

Here I'm trying to use two CPAN modules in a script. 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.

Look at the documentation for 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. Just start with Expect from the very beginning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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