skip to Main Content

I would like to connect via ssh to my router.

SSH connection from shell works fine!

The program:

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();
}

The debug-output:

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> 

When executing the $av_obj_SSH->open2pty I get the message:

Connection to f42240te closed by remote host.

How to prevent this sudden disconnect? Via SSH shell command the connection works as a charm!

Update !! Update:

On the hint of @salva I have changed the code:

  $Net::OpenSSH::debug = ~0;
  $av_obj_SSH = Net::OpenSSH->new($av_loc_ROUTER, user => 'root', key_path => ".ssh/t_zertifikat_a_karl-heinz_fischbach.key");
  $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()
    or die "unable to run open2pty" . $av_obj_SSH->error . "n";
  
  $av_obj_EXPECT = Expect->init($av_loc_PTY);
  $av_obj_EXPECT->raw_pty(1);
  $av_obj_EXPECT->debug(2);
  $av_obj_EXPECT->expect(15, '>')
    or die "expect failedn";
  print "prompt seenn";
  
  $av_obj_EXPECT->send("lsn");
  print "command sentn";
  
  ###
  ### the following three lines are according to the example on github for expect
  ###
  
  $av_obj_EXPECT->expect(15, "n")
    or die "expect failedn";
  print "prompt seenn";
  
  while(<$av_loc_PTY>) {
      print "$. $_"
  }
}

The message "prompt seen" comes!

The message "command sent" comes as well!

The following lines are according to the example on github for expect, but they fail!

main::av_ssh(./scripts/av_ssh_03.pl:185):
185:      $av_obj_EXPECT->expect(15, "n")
186:        or die "expect failedn";
  DB<2>
Starting EXPECT pattern matching...
 at /home/avadmin/perl5/lib/perl5/Expect.pm line 614.
        Expect::expect(Expect=GLOB(0x564e2f6c85a8), 15, "x{a}") called at ./scripts/av_ssh_03.pl line 185
        main::av_ssh() called at ./scripts/av_ssh_03.pl line 329
 at /home/avadmin/perl5/lib/perl5/Expect.pm line 614.
        Expect::expect(Expect=GLOB(0x564e2f6c85a8), 15, "x{a}") called at ./scripts/av_ssh_03.pl line 185
        main::av_ssh() called at ./scripts/av_ssh_03.pl line 329
expect failed

The same action on a ssh via putty to the device look like this:

newroot@F42240te:/
> ls

Firmware  MENU:
Other     MENU:
Setup     MENU:
Status    MENU:

newroot@F42240te:/
>

Neither

$av_obj_EXPECT->expect(15, "n")

nor

$av_obj_EXPECT->expect(15, ">")

work.

What could be wrong?

Update 2 ### Upate 2

O.K. I found one issue!

Commands to be sent have to be appended by "r" and not by "n".

Now, when sending the command "lsr", my router returns some lines.

Given by the example of EXPECT, I could now wait for the prompt by:

$av_obj_EXPECT->expect(15, ">")

But I would like to read the output from the router to do something with the output!

I hav tried:

  
  while(<$av_loc_PTY>) 
  {
      print "$_"
  }

But, naturally, the while does not end!

Any recommendations how to read the output and end the while loop?

2

Answers


  1. Chosen as BEST ANSWER

    This now works!

      $av_obj_SSH = Net::OpenSSH->new($av_loc_ROUTER, user => 'newroot', key_path => ".ssh/t_zertifikat_t_test_openssh.key");
      $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()
        or die "### unable to run open2pty" . $av_obj_SSH->error . "n";
      
      $av_obj_EXPECT = Expect->init($av_loc_PTY);
      $av_obj_EXPECT->raw_pty(1);
      $av_obj_EXPECT->expect(15, '>')
        or die "### expect failedn";
      print "### prompt seenn";
      
      $av_obj_EXPECT->send("cd /Setup/IP-Router/IP-Routing-Tabler");
      print "### command sentn";
      
      $av_obj_EXPECT->expect(15, '>')
        or die "### bad commandn";
      print "### prompt seenn";
      
      $av_obj_EXPECT->clear_accum();
      
      $av_obj_EXPECT->send("lsr");
      print "### command sentn";
      
      $av_obj_EXPECT->expect(15, '>')
        or die "### expect failedn";
      print "### prompt seenn";
      
      $av_tmp_STRING = $av_obj_EXPECT->before();
      $av_tmp_STRING =~ s/n|e]0;//g;
      $av_tmp_STRING =~ s/cG/r/g;
      @av_arr_TMP = split("r", $av_tmp_STRING);
      print "###all lines: n@av_arr_TMPn";
      $av_obj_SSH->disconnect();
    
    

    The router to connect to is a lancom device.


  2. Support for SSH in some routers and network equipment in general, is limited and crippled. They may use some custom shell which does not accept running single commands, or in other cases only accept one session per connection.

    Try asking for a new session, and then using Expect for sending and receiving commands over that session:

    ($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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search