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!
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);
1