Net::OpenSSH 与 Expect 连接到路由器

问题描述 投票:0回答:1

我想通过 ssh 连接到我的路由器。

来自 shell 的 SSH 连接工作正常!

节目:

sub av_ssh
{
  $Net::OpenSSH::debug = ~0;
  $av_obj_SSH = Net::OpenSSH->new($av_loc_ROUTER, user => 'newroot', key_path => ".ssh/t_zertifikat_a_karl-heinz_fischbach.key", timeout => 30);
  $av_obj_SSH->error and die "Couldn't establish SSH connection: ". $av_obj_SSH->error;
  
  ($av_loc_PTY, $av_loc_PID) = $av_obj_SSH->open2pty("ls")
    or die "unable to run remote command";
  
  $av_obj_EXPECT = Expect->init($av_loc_PTY);
  
  $av_std_RETVAL = $av_obj_EXPECT->system({ timeout => 30 },'cd /Setup/IP-Router/IP-Routing-Table');
  $av_obj_EXPECT->error and die "command failed: ". $av_obj_EXPECT->error;
  
  $av_obj_SSH->disconnect();
}

调试输出:

perl -d ./scripts/av_ssh_03.pl -l -v 5

Loading DB routines from perl5db.pl version 1.60
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

Name "main::LOG4PERLCONF" used only once: possible typo at ./scripts/av_ssh_03.pl line 231.
 at ./scripts/av_ssh_03.pl line 231.
main::(./scripts/av_ssh_03.pl:66):      my  $av_std_BASENAME=basename($0,".pl");
  DB<1> n
main::(./scripts/av_ssh_03.pl:67):      my  $av_std_DIRNAME=dirname($0); # gibt dann u.U. nur "." wieder
  DB<1> c 160
alle optionen: -l -v 5
2024/12/14 10:54:01 Log::Log4perl ist wohl initialisiert
main::av_ssh(./scripts/av_ssh_03.pl:160):
160:      $Net::OpenSSH::debug = ~0;
  DB<2>
main::av_ssh(./scripts/av_ssh_03.pl:161):
161:      $av_obj_SSH = Net::OpenSSH->new($av_loc_ROUTER, user => 'newroot', key_path => ".ssh/t_zertifikat_t_test_openssh.key", timeout => 30);
  DB<2>
# open_ex: ['ssh','-V']
# io3 mloop, cin: 0, cout: 1, cerr: 0
# io3 fast, cin: 0, cout: 1, cerr: 0
# stdout, bytes read: 60 at offset 0
#> 4f 70 65 6e 53 53 48 5f 38 2e 39 70 31 20 55 62 75 6e 74 75 2d 33 75 62 75 6e 74 75 30 2e 31 30 | OpenSSH_8.9p1 Ubuntu-3ubuntu0.10
#> 2c 20 4f 70 65 6e 53 53 4c 20 33 2e 30 2e 32 20 31 35 20 4d 61 72 20 32 30 32 32 0a             | , OpenSSL 3.0.2 15 Mar 2022.
# io3 fast, cin: 0, cout: 1, cerr: 0
# stdout, bytes read: 0 at offset 60
# leaving _io3()
# _waitpid(61434) => pid: 61434, rc: 0, err:
# OpenSSH version is 8.9p1
# ctl_path: /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, ctl_dir: /home/fxuser2/.libnet-openssh-perl/
# _is_secure_path(dir: /home/fxuser2/.libnet-openssh-perl, file mode: 16832, file uid: 1000, euid: 1000
# _is_secure_path(dir: /home/fxuser2, file mode: 16877, file uid: 1000, euid: 1000
# set_error(0 - 0)
# call args: ['ssh','-o','ServerAliveInterval=10','-o','ControlPersist=no','-2MNx','-i','.ssh/t_zertifikat_t_test_openssh.key','-o','PreferredAuthentications=publickey','-S','/home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726','-l','newroot','f42240te','--']
# master state jumping from _STATE_START to _STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# tracer attached, ssh pid: 61435, tracer pid: 61436
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object not yet found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726, state:_STATE_AWAITING_MUX
# file object found at /home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726
# master state jumping from _STATE_AWAITING_MUX to _STATE_RUNNING
# call args: ['ssh','-O','check','-T','-S','/home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726','-l','newroot','f42240te','--']
# open_ex: ['ssh','-O','check','-T','-S','/home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726','-l','newroot','f42240te','--']
# io3 mloop, cin: 0, cout: 1, cerr: 0
# io3 fast, cin: 0, cout: 1, cerr: 0
# stdout, bytes read: 28 at offset 0
#> 4d 61 73 74 65 72 20 72 75 6e 6e 69 6e 67 20 28 70 69 64 3d 36 31 34 33 35 29 0d 0a             | Master running (pid=61435)..
# io3 fast, cin: 0, cout: 1, cerr: 0
# stdout, bytes read: 0 at offset 28
# leaving _io3()
# _waitpid(61441) => pid: 61441, rc: 0, err:
main::av_ssh(./scripts/av_ssh_03.pl:162):
162:      $av_obj_SSH->error and die "Couldn't establish SSH connection: ".  $av_obj_SSH->error;
  DB<2>
main::av_ssh(./scripts/av_ssh_03.pl:164):
164:      ($av_loc_PTY,  $av_loc_PID) =  $av_obj_SSH->open2pty("ls")
165:        or die "unable to run remote command";
  DB<2>
# call args: ['ssh','-qtt','-S','/home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726','-l','newroot','f42240te','--','ls']
# open_ex: ['ssh','-qtt','-S','/home/fxuser2/.libnet-openssh-perl/f350b28f93594e09c088f89e76f4d726','-l','newroot','f42240te','--','ls']
main::av_ssh(./scripts/av_ssh_03.pl:167):
167:      $av_obj_EXPECT = Expect->init($av_loc_PTY);
  DB<2> 

执行 $av_obj_SSH->open2pty 时,我收到消息:

与 f42240te 的连接被远程主机关闭。

如何防止这种突然断开连接?通过 SSH shell 命令,连接就像一个魅力!

perl ssh openssh
1个回答
0
投票

一般来说,某些路由器和网络设备对 SSH 的支持是有限的并且是残缺的。他们可能会使用一些不接受运行单个命令的自定义 shell,或者在其他情况下每个连接只接受一个会话。

尝试请求一个新会话,然后使用 Expect 通过该会话发送和接收命令:

($av_loc_PTY, $av_loc_PID) = $av_obj_SSH->open2pty() # No cmd here!
  or die "unable to run remote command";
  
$av_obj_EXPECT = Expect->init($av_loc_PTY);
© www.soinside.com 2019 - 2024. All rights reserved.