2014-11-02 70 views
2

我想写一个PERL脚本ssh到远程机器并运行一些tcptraceroutes。我设法很好地处理了SSH部分。然而,tcptraceroute命令需要sudo,这里是我遇到问题的地方。例如:PERL ssh脚本tty问题

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

当我使用的tty => 1周系统的投诉,出现此错误: “不是一个TTY” 当我不使用TTY => 1,则我得到这个: “须藤:对不起,你必须有一个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; 

在此先感谢。

+0

您应该考虑使用[Net :: OpenSSH](https://metacpan.org/pod/Net::OpenSSH)而不是Net :: SSH :: Perl。其文档包含有关如何使用'sudo'的信息。 – salva 2014-11-03 11:38:46

+0

我想我先试了一下,但我今天开始工作的时候会再检查一次。谢谢。 – th3us3r 2014-11-03 16:29:31

+0

我试过这个模块,但是这个PERL模块没有安装,我没有安装它的权限。有什么其他方式可以用PERL来完成这项任务吗? – th3us3r 2014-11-05 01:58:52

回答