2014-03-31 107 views
0

這裏我試圖在腳本中使用兩個CPAN模塊。除了我在代碼中使用的方法之外,是否有正確的方法來使用?如何在腳本中使用兩個CPAN模塊

#!/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"); 

我得到錯誤"Can't locate object method "send" via package "Net::SSH::Perl::SSH2"雖然「送」的方法與相關的「網:: SSH ::期待」的劇本會在「淨:: SSH :: Perl的」方法是不正確。

+0

目前還不清楚你想在這裏做什麼。你不能同時通過兩個包來保護單個引用,這是有道理的 - 單個對象怎樣才能同時屬於兩個不同的類? – raina77ow

+0

基本上我試圖用遠程計算機來使用perl,同時我也需要su以root身份來獲得遠程機器的root權限。 「與Net :: SSH :: Perl相關的我的($ stdout,$ stderr,$ exit)= $ ssh-> cmd($ cmd,[$ stdin])」不工作,因此想要使用「send」 Net :: SSH :: Expect。在嘗試此操作之前,我想檢查Net :: SSH :: Expect的功能,因此使用官方頁面中提供的默認代碼。我是perl的新手,所以不確定這是否可以完成。好心提醒。 – Karthik

+1

您可以使用Net :: SSH :: Expect登錄,請通過聯機幫助頁。 – Pradeep

回答

2

查看Net::SSH::Expect的文檔。它顯示瞭如何創建對象的實例,登錄等。

您無法通過Net::SSH::Perl登錄並將您的實例$ssh奇蹟般地轉換爲Expect對象。從一開始就從Expect開始。

+0

謝謝,米勒。問題是我找不到在Net :: SSH :: Perl中可用的方法「$ ssh-> shell」的替代方法。我需要使用此腳本以及登錄自動化替換SSH客戶端。 – Karthik