dayton has asked for the wisdom of the Perl Monks concerning the following question:

Using Net::SSH::Perl and I can't establish a connection even though my key is accepted... what am I missing?
.ssh/config:

	Host remote.ssh.server
    	ControlMaster auto
    	ControlPath ~/.ssh/master-%r@%h:%p
    	User myusername
    	IdentityFile id_rsa

The Code:

$ssh = Net::SSH::Perl->new($host,debug=>true,options => ["ConnectT +imeout=5","StrictHostKeyChecking=no"]); $ssh_conf="$ENV{'HOME'}/.ssh/config"; $cfg = Net::SSH::Perl::Config->new($host); $cfg->read_config($ssh_conf); if (! $USERNAME){ $USER=$cfg->get('user'); } eval{$ssh->login($USER);}; if ($@){ print "Unable to connect to $host\n$@\n"; } eval{($out, $err, $exit)=$ssh->cmd($exec_cmd);};print "Unable to e +xecute: $exec_cmd\n" if $@; ### print "\n\nmade it this far\n\n"; ### if ($out){ print $out; } else { print "$err\n$exit"; }

result:

local.host: Reading configuration data /home/myusername/.ssh/config
local.host: Reading configuration data /etc/ssh_config
local.host: Connecting to remote.ssh.server, port 22.
local.host: Remote version string: SSH-2.0-OpenSSH_7.2
local.host: Remote protocol version 2.0, remote software version OpenSSH_7.2
local.host: Net::SSH::Perl Version 2.09, protocol version 2.0.
local.host: No compat match: OpenSSH_7.2.
local.host: Connection established.
local.host: Sent key-exchange init (KEXINIT), waiting for response.
local.host: Using curve25519-sha256@libssh.org for key exchange
local.host: Host key algorithm: ssh-ed25519
local.host: Algorithms, c->s: chacha20-poly1305@openssh.com <implicit> none
local.host: Algorithms, s->c: chacha20-poly1305@openssh.com <implicit> none
local.host: Generating ephemeral key pair.
local.host: Entering Curve 25519 Key Exchange.
local.host: Sent client public key, waiting for reply.
local.host: Received host key, type 'ssh-ed25519'.
local.host: Host 'remote.ssh.server' is known and matches the host key.
local.host: Verifying server signature.
local.host: Send NEWKEYS.
local.host: Waiting for NEWKEYS message.
local.host: Enabling encryption/MAC/compression.
local.host: Sending request for user-authentication service.
local.host: SSH2_MSG_EXT_INFO received
local.host: SSH Extension activated: server-sig-algs=rsa-sha2-256,rsa-sha2-512
local.host: Service accepted: ssh-userauth.
local.host: Trying empty user-authentication request.
local.host: Authentication methods that can continue: publickey,gssapi-keyex,gssapi-with-mic,password.
local.host: Next method to try is publickey.
local.host: Publickey: testing agent key '/home/myusername/.ssh/id_rsa'
local.host: Public key is accepted, signing data.
local.host: Key fingerprint: SHA256:rScQOC1zgitfa/IiDb7CYwPkwK/k16ViRuAfzv4ITcQ=
Unable to connect to remote.ssh.server
Error reading response length from auth socket. at /usr/local/lib64/perl5/Net/SSH/Perl/Auth/PublicKey.pm line 104.

local.host: channel 0: new client-session
local.host: Requesting channel_open for channel 0.
local.host: Entering interactive session.


made it this far
]$

just using "ssh -v":

]$ ssh -v remote.ssh.server "uptime"
OpenSSH_7.5p1, OpenSSL 1.1.0f-fips  25 May 2017
debug1: Reading configuration data /home/myusername/.ssh/config
debug1: /home/myusername/.ssh/config line 8: Applying options for remote.ssh.server
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Reading configuration data /etc/ssh/ssh_config.d/05-redhat.conf
debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
debug1: /etc/ssh/ssh_config.d/05-redhat.conf line 8: Applying options for *
debug1: auto-mux: Trying existing master
debug1: Control socket "/home/myusername/.ssh/master-myusername@remote.ssh.server:22" does not exist
debug1: Connecting to remote.ssh.server xxx.xxx.xxx.xxx port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.5
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2
debug1: match: OpenSSH_7.2 pat OpenSSH* compat 0x04000000
debug1: Authenticating to remote.ssh.server:22 as 'myusername'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: aes256-gcm@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: aes256-gcm@openssh.com MAC: <implicit> compression: none
debug1: kex: curve25519-sha256@libssh.org need=32 dh_need=32
debug1: kex: curve25519-sha256@libssh.org need=32 dh_need=32
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:b0EOVxSO46RlNAPAeDQg66CnCsKupSxhB1Kl5ihJ9Ok
debug1: Host 'remote.ssh.server' is known and matches the ECDSA host key.
debug1: Found key in /home/myusername/.ssh/known_hosts:3
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:1000)

debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:1000)

debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/myusername/.ssh/id_rsa
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
debug1: Authentication succeeded (publickey).
Authenticated to remote.ssh.server (xxx.xxx.xxx.xxx:22).
debug1: setting up multiplex master socket
debug1: channel 4: new /home/myusername/.ssh/master-myusername@remote.ssh.server:22
debug1: channel 5: new client-session
debug1: Entering interactive session.
debug1: pledge: id
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Requesting X11 forwarding with authentication spoofing.
debug1: Requesting authentication agent forwarding.
debug1: Sending environment.
debug1: Sending env LANG = en_US.utf8
debug1: Sending env XMODIFIERS = @im=none
debug1: Sending command: uptime
debug1: client_input_channel_req: channel 5 rtype exit-status reply 0
debug1: client_input_channel_req: channel 5 rtype eow@openssh.com reply 0
 15:23:01 up 50 days, 19:36,  1 user,  load average: 0.45, 0.25, 0.24
debug1: channel 5: free: client-session, nchannels 6
debug1: channel 0: free: port listener, nchannels 5
debug1: channel 1: free: port listener, nchannels 4
debug1: channel 2: free: port listener, nchannels 3
debug1: channel 3: free: port listener, nchannels 2
debug1: channel 4: free: /home/myusername/.ssh/master-myusername@remote.ssh.server:22, nchannels 1
Transferred: sent 2744, received 2564 bytes, in 0.1 seconds
Bytes per second: sent 27909.5, received 26078.7
debug1: Exit status 0

Replies are listed 'Best First'.
Re: Issue with Net::SSH::Perl - can't establish connection
by salva (Canon) on Jul 19, 2017 at 06:00 UTC
    You have two variables $USER and $USERNAME that probably should be just one. Use strict!
      There's a reason I have $USER and $USERNAME, but I see your point and I'll look at combining them. But that doesn't explain why the script is failing... It's reading my .ssh/config, pulling the correct key/user combo and sends them to the remote server. The remote server accepts the key, but then the script dies with the error...
        Error reading response length from auth socket. at /usr/local/lib64/perl5/Net/SSH/Perl/Auth/PublicKey.pm line 104.

        That looks like the module is connecting to the local ssh agent and getting an unexpected response. Are your keys protected by a passphrase?

        I would run both the Perl script and the OpenSSH client with strace and see what both were doing different at the point of failure.