Skip Menu |

This queue is for tickets about the Net-OpenSSH CPAN distribution.

Report information
The Basics
Id: 133191
Status: open
Priority: 0/
Queue: Net-OpenSSH

People
Owner: Nobody in particular
Requestors: Andrew [...] DeFaria.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: $ssh->error does not seem to detect address in use
Date: Wed, 19 Aug 2020 09:48:57 -0700
To: bug-Net-OpenSSH [...] rt.cpan.org
From: Andrew DeFaria <Andrew [...] DeFaria.com>
For some reason Net::OpenSSH does not properly handle the case where the address is in use. Shown here in the Perl debugger I first set a breakpoint for line #79 and run to it. Next I continue to line #87. At this point the ssh tunnel has been established and is up and running. Now, to exhibit the problem, I again try to set up an exact duplicate of the tunnel. I get "Address already in use" written to stderr but as you can see $ssh2->error returns 0 as if there is no error. I'm trying to catch the situation where the tunnel is already up. 78     sub exhibitBug() { 79==>b   my $tunnelStr = "-NLlocalhost:1025:defaria.com:25"; 80 81       # First establish the tunnel 82:      my $ssh1 = Net::OpenSSH->new('defaria.com', master_opts => $tunnelStr); 83 84:      $ssh1->error and die "Couldn't set up tunnel - " . $ssh1->error; 85 86       # Now try to establish the tunnel again. 87:      my $ssh2 = Net::OpenSSH->new('defaria.com', master_opts => $tunnelStr); 88 89:      $ssh2->error and die "Couldn't set up tunnel - " . $ssh2->error; 90 91       # No error is reported! 92:      return; 93     }   DB<<7>> c 87 main::exhibitBug(tunnel.pl:87):      my $ssh2 = Net::OpenSSH->new('defaria.com', master_opts => $tunnelStr);   DB<<8>> # Now ssh tunnel is established   DB<<9>> c 89 bind [127.0.0.1]:1025: Address already in use channel_setup_fwd_listener_tcpip: cannot listen to port: 1025 Could not request local forwarding. main::exhibitBug(tunnel.pl:89):      $ssh2->error and die "Couldn't set up tunnel - " . $ssh2->error;   DB<<10>> x $ssh2->error 0  0   DB<<11>> What's worse is that multiple ssh sessions are established, even though the address was in use! Earth:ps -ef | grep '\-NL' andrew    180966  180818  0 09:42 pts/2    00:00:00 ssh -NLlocalhost:1025:defaria.com:25 -o ServerAliveInterval=30 -o ControlPersist=no -2MNx -S /home/andrew/.libnet-openssh-perl/ef9bb48494b15dfdeeffddee2b7253e4 defaria.com -- andrew    181006  180818  0 09:43 pts/2    00:00:00 ssh -NLlocalhost:1025:defaria.com:25 -o ServerAliveInterval=30 -o ControlPersist=no -2MNx -S /home/andrew/.libnet-openssh-perl/46fd3cf2cb458d425e55b7de451c4e2a defaria.com -- Earth: Is there something I can do or set so that the second tunnel fails to start another ssh session and returns some sort of error code in $ssh->error? -- Andrew DeFaria <http://defaria.com> If a pig loses its voice, is it disgruntled?
Subject: Re: [rt.cpan.org #133191] $ssh->error does not seem to detect address in use
Date: Wed, 19 Aug 2020 21:07:30 +0200
To: bug-Net-OpenSSH [...] rt.cpan.org
From: Salvador Fandiño <sfandino [...] yahoo.com>
On 19/8/20 18:58, Andrew DeFaria via RT wrote: Show quoted text
> Wed Aug 19 12:58:46 2020: Request 133191 was acted upon. > Transaction: Ticket created by Andrew@DeFaria.com > Queue: Net-OpenSSH > Subject: $ssh->error does not seem to detect address in use > Broken in: (no value) > Severity: (no value) > Owner: Nobody > Requestors: Andrew@DeFaria.com > Status: new > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=133191 > > > > For some reason Net::OpenSSH does not properly handle the case where the > address is in use. Shown here in the Perl debugger I first set a > breakpoint for line #79 and run to it. Next I continue to line #87. At > this point the ssh tunnel has been established and is up and running. > Now, to exhibit the problem, I again try to set up an exact duplicate of > the tunnel. I get "Address already in use" written to stderr but as you > can see $ssh2->error returns 0 as if there is no error. I'm trying to > catch the situation where the tunnel is already up. > > 78     sub exhibitBug() { > 79==>b   my $tunnelStr = "-NLlocalhost:1025:defaria.com:25"; > 80 > 81       # First establish the tunnel > 82:      my $ssh1 = Net::OpenSSH->new('defaria.com', master_opts => > $tunnelStr); > 83 > 84:      $ssh1->error and die "Couldn't set up tunnel - " . > $ssh1->error; > 85 > 86       # Now try to establish the tunnel again. > 87:      my $ssh2 = Net::OpenSSH->new('defaria.com', master_opts => > $tunnelStr); > 88 > 89:      $ssh2->error and die "Couldn't set up tunnel - " . > $ssh2->error; > 90 > 91       # No error is reported! > 92:      return; > 93     } >   DB<<7>> c 87 > main::exhibitBug(tunnel.pl:87):      my $ssh2 = > Net::OpenSSH->new('defaria.com', master_opts => $tunnelStr); >   DB<<8>> # Now ssh tunnel is established > >   DB<<9>> c 89 > bind [127.0.0.1]:1025: Address already in use > channel_setup_fwd_listener_tcpip: cannot listen to port: 1025 > Could not request local forwarding. > main::exhibitBug(tunnel.pl:89):      $ssh2->error and die "Couldn't > set up tunnel - " . $ssh2->error; >   DB<<10>> x $ssh2->error > 0  0 >   DB<<11>> > > What's worse is that multiple ssh sessions are established, even though > the address was in use! > > Earth:ps -ef | grep '\-NL' > andrew    180966  180818  0 09:42 pts/2    00:00:00 ssh > -NLlocalhost:1025:defaria.com:25 -o ServerAliveInterval=30 -o > ControlPersist=no -2MNx -S > /home/andrew/.libnet-openssh-perl/ef9bb48494b15dfdeeffddee2b7253e4 > defaria.com -- > andrew    181006  180818  0 09:43 pts/2    00:00:00 ssh > -NLlocalhost:1025:defaria.com:25 -o ServerAliveInterval=30 -o > ControlPersist=no -2MNx -S > /home/andrew/.libnet-openssh-perl/46fd3cf2cb458d425e55b7de451c4e2a > defaria.com -- > Earth: > > Is there something I can do or set so that the second tunnel fails to > start another ssh session and returns some sort of error code in > $ssh->error?
The openssh client wrapped by Net::OpenSSH just ignores the failure to open a tunnel and keeps going so the module can not catch that kind of errors. There are two workarounds for that. One is to redirect the master stderr stream to a file and look there for the warnings telling you the tunnel setup has failed. The other is to open the tunnel after the connection has been established, see the section "Port forwarding" in the module documentation (though, I not sure if ssh reports failures to create the tunnel when it is used that way either).