Net::OpenSSH with Expect to connect to router

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật