我已经尝试了 perl 模块 Net::SSH:Perl 和 Net::OpenSSH 无济于事。 X11 转发确实有效,因为如果我执行“ssh root@host”并执行 X 应用程序(例如“xterm”),我会返回一个窗口。
以下是我尝试过的一些方法:
$self->{'ssh'} = Net::OpenSSH->new("root:PW@".$hostname);
print $self->{'ssh'}->capture("env"); #The display variable is not set so it won't work
print $self->{'ssh'}->capture("xterm");
没有
$self->{'ssh'} = Net::OpenSSH->new("root:PW@".$hostname, master_opts => ['-X' => '']);
print $self->{'ssh'}->capture("env"); #The display variable is not set so it won't work
print $self->{'ssh'}->capture("xterm"); #Nope
print $self->{'ssh'}->capture({master_opts => ['-X']}, "xterm"); #Nope
不,现在是 Net::SSH::Perl
$self->{'ssh'} = Net::SSH::Perl->new("$hostname", debug=>0);
$self->{'ssh'}->login("root","pass");
my ($stdout, $stderr, $exit) = $self->{'ssh'}->cmd("xterm"); #Nope
没有
$self->{'ssh'} = Net::SSH::Perl->new("$hostname", debug=>0, options=>["ForwardX11 yes"]);
$self->{'ssh'}->login("root","pass");
my ($stdout, $stderr, $exit) = $self->{'ssh'}->cmd("xterm"); #Nope
唯一真正有效的是如果我执行以下操作,那么我知道 X11 转发在 Perl 中工作。
`ssh root@host xterm`
如果可能的话,我宁愿让模块工作,但如果我能以某种方式打开双向管道,与 SSH 通信并在我想要的时候接收数据(类似于我如何 $self->{'ssh'}-> cmd() 并在我的脚本中随时接收输出),我会这样做。我只是不知道从哪里开始。以前有人这样做过吗?
最佳答案
Net::OpenSSH
的开发版本有一个新选项forward_X11
。这似乎有效:
my $ssh = Net::OpenSSH->new("localhost", forward_X11 => 1);
print $ssh->capture({forward_X11 => 1}, "env"); # includes DISPLAY=localhost...
print $ssh->capture({forward_X11 => 1}, "xclock"); # starts the xclock program
请注意,您必须在构造函数和实际命令上指定新选项。
关于perl - Perl 中的 SSH 与 X11 转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17700963/