Perl SSH连接执行telnet

标签 perl ssh telnet tunnel

我尝试以下方法通过中央管理服务器作为“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/

相关文章:

perl - 使用 Apache 和 Perl 进行服务器推送的机制

ssh - 将公钥发送到远程主机的命令

ssh - 如何设置 SSH 隧道以访问防火墙后面的网络服务器?

udp - Telnet 到已被使用的端口

没有用户名登录的 Ansible telnet

perl - 如何在 Perl 中用子类覆盖父类函数?

perl - XML::Parser 拒绝安装

linux - 在一台机器上模拟telnet通信

perl - 修改Moose属性方法

bash 序列 : wait for output, 然后开始下一个程序