gpt4 book ai didi

PERL ssh 脚本 tty 问题

转载 作者:行者123 更新时间:2023-12-02 14:03:27 24 4
gpt4 key购买 nike

我正在尝试编写一个 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;

提前致谢。

最佳答案

完全可以在您的主目录中安装一个模块并在您的脚本中调用它。具体步骤请参见 Perlmonks 上的此节点:Installing Perl modules in home directory

关于PERL ssh 脚本 tty 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26702008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com