我尝试以下方法通过中央管理服务器作为“ssh hop”服务器访问路由器
#!/usr/bin/perl -X
use strict;
use Net::OpenSSH;
use Net::Telnet;
my $lhost = "linuxserver";
my $luser = "linuxuser";
my $lpass = "linuxpassword";
my $chost = "routername";
my $cpass = "Routerpassword";
my $prompt = '/(?:Password: |[>])/m';
my @commands = ("show users\r");
my $ssh = Net::OpenSSH->new($lhost,
'user' => $luser,
'password' => $lpass,
'master_opts' => [ '-t' ],
#'async' => 1 # if enabled then password cannot be set here
);
my ($pty, $err, $pid) = $ssh->open2pty("telnet $chost");
my $t = new Net::Telnet(
-telnetmode => 0,
-fhopen => $pty,
-prompt => $prompt,
-cmd_remove_mode => 1,
-output_record_separator => "\r",
#-dump_log => "debug.log",
);
my $end = 0;
while (!$end) {
my ($pre, $post) = $t->waitfor($prompt);
if ($post =~ /Password: /m) {
# send password
$t->print("$cpass");
}
elsif ($post =~ /[>#]/ && @commands) {
my $cmd = shift(@commands);
if ($cmd !~ /[\r\n]/) {
$t->print($cmd);
}
else {
print $t->cmd($cmd);
}
}
else {
$end = 1;
$t->cmd("exit");
}
}
#close $pty;
$t->close();
不幸的是,我总是收到以下错误: 读取错误:test.pl 第 71 行输入/输出错误
有人可以帮助我吗?或者是否有更好的解决方案来测试是否可以通过“hop”服务器进行 telnet 连接?
连接如下: 工作站--ssh->服务器--telnet->路由器
提前致谢。
最佳答案
我认为最好的选择是建立一个通往管理服务器的 SSH 隧道,并使用它来远程登录到路由器。
关于Perl SSH连接执行telnet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5622895/