Perl SSH连接执行telnet

发布于 2024-10-31 08:12:04 字数 1388 浏览 1 评论 0原文

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

提前致谢。

I tried the following to access a router via a central admin server as "ssh hop" server


#!/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();

Unfortunately I always get the following error:
read error: Input/output error at test.pl line 71

Can somebody help me please or is there a better solution only to test if a telnet connection via the "hop" server is possible or not?

The connection looks like:
workstation --ssh-> server --telnet-> router

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花期渐远 2024-11-07 08:12:04

我认为最好的选择是建立一个连接到管理服务器的 SSH 隧道,并使用它来远程登录到路由器。

I think best option is to make an SSH-tunnel to your admin server and use it for telnetting to the router.

萝莉病 2024-11-07 08:12:04

Net::Telnet 通过 Net::OpenSSH 有时并不像应有的那么容易,它需要一些实验才能获得正确的标志组合和使其工作的电话。

例如,不要远程登录到目标主机,而是使用 netcat 打开原始连接(或 Net ::OpenSSH 支持 TCP 转发(如果代理上允许隧道)。

预期 + Net::OpenSSH 可能是更好的选择。

Getting Net::Telnet to work over Net::OpenSSH sometimes is not as easy as it should be and it requires some experimentation to get to the right combination of flags and calls that make it work.

For instance, instead of telneting to the target host, use netcat to open a raw connection (or Net::OpenSSH support for TCP forwarding if tunnels are allowed on the proxy).

Expect + Net::OpenSSH may be a better option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文