【问题标题】:PERL ssh script tty issuePERL ssh 脚本 tty 问题
【发布时间】:2014-12-29 09:17:29
【问题描述】:

我正在尝试编写一个 PERL 脚本来通过 ssh 连接到远程机器并运行一些 tcptraceroutes。我设法很好地处理了 SSH 部分。但是 tcptraceroute 命令需要 sudo,这就是我遇到问题的地方。示例:

my (@traces, $stderr, $exit) = $sshCali->cmd(tty => 1, 
"sudo tcptraceroute $endpoint", interactive => 1, debug => 1);

当我使用 tty => 1 系统投诉时出现此错误: “不是 tty” 当我不使用 tty => 1 时,我得到这个: “sudo:对不起,你必须有一个 tty 才能运行 sudo” 任何帮助将不胜感激。这是我的代码:

#!/usr/bin/perl
use strict;
use warnings; 
use Net::SSH::Perl;

#vars
my $endpoint = 172.31.100.1;
my @traces;
my $sshCali;
my $hostCali = 'remotemachine';
my $cmd = 'sudo traceroute';

warn "Starting SSH Services:...\n";

$sshCali = Net::SSH::Perl->new($hostCali, interactive => 1, debug => 0) or die "Couldnt establish connection!";

$sshCali -> login("user"); # for non-interactive mode;

print "here\n";

my (@traces, $stderr, $exit) = $sshCali->cmd("sudo tcptraceroute $endpoint", interactive => 1, debug => 1);

#my (@traces, $stderr, $exit) = $sshCali->cmd(tty => 1,"$cmd $endpoint", interactive => 1, debug => 1);

print STDERR "STDERR: $stderr\n" if $stderr;
print "command exit w/ code: $exit\n";

$sshCali -> cmd("exit");


print "NOW ..... Printing the array: TRACES\n";
print @traces;

提前致谢。

【问题讨论】:

  • 您应该考虑使用Net::OpenSSH 而不是 Net::SSH::Perl。它的文档包含有关如何将其与sudo 一起使用的信息。
  • 我想我一开始尝试了一下,但我会在今天上班时再次检查。谢谢。
  • 我试过这个模块,但是这个 PERL 模块没有安装,我没有安装它的权限。有没有其他方法可以用 PERL 完成这项任务?

标签: perl ssh sudo tty traceroute


【解决方案1】:

完全可以在你的主目录中安装一个模块并在你的脚本中调用它。有关确切步骤,请参阅 Perlmonks 上的此节点:Installing Perl modules in home directory

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多