2014-11-02 73 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

回答