【问题标题】:How to use two CPAN modules in a script如何在脚本中使用两个 CPAN 模块
【发布时间】:2014-03-31 15:32:53
【问题描述】:

这里我试图在一个脚本中使用两个 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" 尽管“发送”方法与“Net::SSH::Expect”相关联,但脚本会在“Net::SSH::Perl”中查找不正确的方法。

【问题讨论】:

  • 不清楚你想在这里做什么。您不能同时通过两个包来祝福单个引用,这是有道理的 - 单个对象如何同时属于两个不同的类?
  • 基本上我正在尝试使用 perl ssh 到远程机器,我还需要 su 到 root 以获得远程机器上的 root 访问权限。与 Net::SSH::Perl 关联的“my($stdout, $stderr, $exit) = $ssh->cmd($cmd,[$stdin])” 不起作用,因此想使用“发送”方法网络::SSH::期望。在尝试此之前,我想检查 Net::SSH::Expect 的功能,因此使用了官方页面中提供的默认代码。我是 perl 的新手,所以不确定这是否可以完成。请多多指教。
  • 您可以使用 Net::SSH::Expect 登录,请通过手册页。
  • 嘿Karthik,你至少应该用use strict开始你的perl脚本;使用警告;有关“-w”和“使用警告”之间的差异,请参阅 stackoverflow.com/a/12554195/1702521

标签: linux perl ssh


【解决方案1】:

查看Net::SSH::Expect 的文档。它展示了如何创建对象的实例、登录等。

您无法通过Net::SSH::Perl 登录并神奇地将您的实例$ssh 转换为Expect 对象。从一开始就从 Expect 开始。

【讨论】:

  • 谢谢,米勒。问题是我找不到在 Net::SSH::Perl 中可用的方法“$ssh->shell”的替代方法。我需要使用此脚本以及登录自动化来替换 SSH 客户端。
猜你喜欢
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
  • 2017-01-02
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 1970-01-01
相关资源
最近更新 更多