perlopenssh

Net::OpenSSH not running the command?


I'm trying to get started using the Net::OpenSSH module and am running into issues. I've tried a few different methods I've found online, but I dont seem to be able to get the output from a command on the remote device (nokia routers in this case).

I do see the login 'message' (warning about unauthorized access etc) when connecting but do not see any login prompts (should they be visible with Net::OpenSSH?).

I then see this message "Connection to closed by remote host".

I cant tell what its doing since it isnt outputting anything else to the screen and it is not populating $fh. Any ideas?

#!/usr/bin/perl

use strict;
use warnings;

use Net::OpenSSH;
my $ip = "";
my $username = "";
my $passwd = "";

            my $ssh = connect_to_device($ip);
            my $fh = $ssh->pipe_out('show card state') or die "unable to run command\n";
            while (<$fh>) {
                    print "$_";  
            }
            close $fh;
            undef $ssh;
    
    sub connect_to_device {
            my $deviceIpAddr = shift;
            $Net::OpenSSH::debug = ~0;  # Enable comprehensive debug output
            my $ssh = Net::OpenSSH->new($deviceIpAddr,
                    user => $username,
                    password => $passwd,
                    timeout => 3,
                    strict_mode => 0,
                    master_opts => ['-o UserKnownHostsFile=/dev/null']
            );
            $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;
            return $ssh;
    }

Debug Output:

# open_ex: ['ssh','-V']
# io3 mloop, cin: 0, cout: 1, cerr: 0
# io3 fast, cin: 0, cout: 1, cerr: 0
# stdout, bytes read: 48 at offset 0
#> 4f 70 65 6e 53 53 48 5f 37 2e 34 70 31 2c 20 4f 70 65 6e 53 53 4c 20 31 2e 30 2e 32 6b 2d 66 69 | OpenSSH_7.4p1, OpenSSL 1.0.2k-fi
#> 70 73 20 20 32 36 20 4a 61 6e 20 32 30 31 37 0a                                                 | ps  26 Jan 2017.
# io3 fast, cin: 0, cout: 1, cerr: 0
# stdout, bytes read: 0 at offset 48
# leaving _io3()
# _waitpid(14149) => pid: 14149, rc: 0, err: 
# OpenSSH version is 7.4p1,
# ctl_path: /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, ctl_dir: /root/.libnet-openssh-perl/
# set_error(0 - 0)
# call args: ['ssh','-o UserKnownHostsFile=/dev/null','-o','ServerAliveInterval=10','-o','ControlPersist=no','-2MNx','-o','NumberOfPasswordPrompts=1','-o','PreferredAuthentications=keyboard-interactive,password','-S','/root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad','-l','<username>','<ipaddress>','--']
# master state jumping from _STATE_START to _STATE_LOGIN
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# tracer attached, ssh pid: 14150, tracer pid: 14151
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
Warning: Permanently added '<ipaddress>' (RSA) to the list of known hosts.
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
TiMOS-C-16.0.R11 cpm/hops64 Nokia 7450 ESS Copyright (c) 2000-2020 Nokia.
All rights reserved. All use subject to applicable license agreements.
Built on Wed Jan 29 11:57:40 PST 2020 by builder in /builds/c/160B/R11/panos/main
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_LOGIN
# passwd/passphrase requested (<username>@<ipaddress>'s password:)
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_AWAITING_MUX
# file object not yet found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad, state:_STATE_AWAITING_MUX
# file object found at /root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad
# master state jumping from _STATE_AWAITING_MUX to _STATE_RUNNING
# call args: ['ssh','-O','check','-T','-S','/root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad','-l','<username>@<ipaddress>','--']
# open_ex: ['ssh','-O','check','-T','-S','/root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad','-l','<username>@<ipaddress>','--']
# 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 31 34 31 35 30 29 0d 0a             | Master running (pid=14150)..
# io3 fast, cin: 0, cout: 1, cerr: 0
# stdout, bytes read: 0 at offset 28
# leaving _io3()
# _waitpid(14161) => pid: 14161, rc: 0, err: 
# call args: ['ssh','-S','/root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad','-l','<username>@<ipaddress>','--','show card state']
# pipe_out: 'ssh''-S''/root/.libnet-openssh-perl/7d29d81a517f00787fb9ba9ca54353ad''-l''<username>@<ipaddress>''--''show card state'
Connection to <ipaddress> closed by remote host.
# DESTROY(Net::OpenSSH=HASH(0xd7fd58), pid: 14150)
# sending exit control to master
# set_error(1 - control command failed: master SSH connection broken)
# master state jumping from _STATE_RUNNING to _STATE_KILLING
# killing master
# _master_kill: 14150
# waitpid(master: 14150) => pid: 0, rc: No such file or directory
# waitpid(master: 14150) => pid: 14150, rc: Interrupted system call
# master state jumping from _STATE_KILLING to _STATE_GONE
# master state jumping from _STATE_GONE to _STATE_GONE

Solution

  • To debug this, try enable debug output from Net::OpenSSH. For example:

    use v5.38;
    use Net::OpenSSH;
    
    $Net::OpenSSH::debug = ~0;  # Enable comprehensive debug output
    my $user = "hakonh";
    my $host = "172.17.0.2";
    my $port = 2222;
    my $ssh = Net::OpenSSH->new(
        $host,
        user => $user,
        port => $port,
        passwd => '????'
    );
    $ssh->error and
      die "Couldn't establish SSH connection: ". $ssh->error;
    
    say "Successfully connected ";
    
    # Shut down the connection by letting the `$ssh` object go out of scope
    

    Please update your question with debug output like this. This can help us narrow down the problem.