For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2005 > Net::SSH::Perl bind socket problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Net::SSH::Perl bind socket problem
Gui

2005-04-06, 12:40 pm

hello,

I'm making a simple script that's supposed to update a certain number of
boxes, spread around the area.
my script is supposed to to connect to every hosts (via a file named
"hosts") using ssh, launch wget to retrieve the patch update, and
install it.
Things work fine for the first host of the list. But can never get
beyong that. I get a "Net::SSH: Can't bind socket to port 1023: Adresse
déjà utilisée at ./test_ssh.pl line 46" message.

my question is, how can I unbind the socket used by Net::SSH::Perl ?

here's part of my code:

use strict;
use Net::SSH::Perl;

my ($patchurl,$stout,$sterr,$exit,$ssh);
my @ids = "$ENV{HOME}/.ssh/id_rsa";

my %params = (
'protocol' => 2,
'identity_files' => \@ids,
);

$patchurl = push @ARGV

open HOSTS, "hosts" or die "je n'ai pas réussi à ouvrir le fichier hosts
: $!";

while(<HOSTS> ){

(...)

$ssh = Net::SSH::Perl->new($_, %params);
$ssh->login("root");
($stout,$sterr,$exit) = $ssh->cmd("wget -q $patchurl");

(...)
}


thanks
mgoland@optonline.net

2005-04-06, 12:40 pm



----- Original Message -----
From=3A gui =3Cgdoute=40tele2=2Efr=3E
Date=3A Monday=2C April 4=2C 2005 3=3A16 pm
Subject=3A Net=3A=3ASSH=3A=3APerl bind socket problem

=3E hello=2C
=3E =

=3E I=27m making a simple script that=27s supposed to update a certain =

=3E number of =

=3E boxes=2C spread around the area=2E
=3E my script is supposed to to connect to every hosts (via a file =

=3E named =

=3E =22hosts=22) using ssh=2C launch wget to retrieve the patch update=2C=
and =

=3E install it=2E
=3E Things work fine for the first host of the list=2E But can never get =

=3E beyong that=2E I get a =22Net=3A=3ASSH=3A Can=27t bind socket to port=
1023=3A =

=3E Adresse =

=3E d=E9j=E0 utilis=E9e at =2E/test=5Fssh=2Epl line 46=22 message=2E
=3E =

=3E my question is=2C how can I unbind the socket used by Net=3A=3ASSH=3A=
=3APerl =3F
not sure about the answare=2C but why dont you try to debug the comunicat=
ion


=3E =

=3E here=27s part of my code=3A
=3E =

=3E use strict=3B
=3E use Net=3A=3ASSH=3A=3APerl=3B
=3E =

=3E my (=24patchurl=2C=24stout=2C=24sterr=2C=24
exit=2C=24ssh)=3B
=3E my =40ids =3D =22=24ENV=7BHOME=7D/=2Essh/id=5Frsa=22=3B
=3E =

=3E my =25params =3D (
=3E =27protocol=27 =3D=3E 2=2C
=3E =27identity=5Ffiles=27 =3D=3E =5C=40ids=2C

=27port=27 =3D=3E 22=2C
=27debug=27 =3D=3E 1=2C


=3E )=3B
=3E =

=3E =24patchurl =3D push =40ARGV
=3E =

=3E open HOSTS=2C =22hosts=22 or die =22je n=27ai pas r=E9ussi =E0 ouvrir=
le fichier =

=3E hosts =

=3E =3A =24!=22=3B
=3E =

=3E while(=3CHOSTS=3E)=7B
=3E =

=3E (=2E=2E=2E)
=3E =

=3E =24ssh =3D Net=3A=3ASSH=3A=3APerl-=3Enew(=24=5F=2C =25params)=
=3B
=3E =24ssh-=3Elogin(=22root=22)=3B
=3E (=24stout=2C=24sterr=2C=24exit) =3D =24ssh-=3Ecmd(=22wget -q =
=24patchurl=22)=3B
=3E =

=3E (=2E=2E=2E)
=3E =7D
=3E =

=3E =

=3E thanks
=3E =

=3E -- =

=3E To unsubscribe=2C e-mail=3A beginners-unsubscribe=40perl=2Eorg
=3E For additional commands=2C e-mail=3A beginners-help=40perl=2Eorg
=3E =3Chttp=3A//learn=2Eperl=2Eorg/=3E =3Chttp=3A//learn=2Eperl=2Eorg/fir=
st-response=3E
=3E =

=3E =

=3E

Gui

2005-04-06, 12:41 pm

added debug in the params. Can't find any real clue on what to do
though, here's what I got :

ubuntu: Reading configuration data /home/gui/.ssh/config
ubuntu: Reading configuration data /etc/ssh_config
ubuntu: Allocated local port 1023.
ubuntu: Connecting to 192.168.0.1, port 22.
ubuntu: Remote protocol version 2.0, remote software version OpenSSH_3.9p1
ubuntu: Net::SSH::Perl Version 1.27, protocol version 2.0.
ubuntu: No compat match: OpenSSH_3.9p1.
ubuntu: Connection established.
ubuntu: Sent key-exchange init (KEXINIT), wait response.
ubuntu: Algorithms, c->s: 3des-cbc hmac-sha1 none
ubuntu: Algorithms, s->c: 3des-cbc hmac-sha1 none
ubuntu: Entering Diffie-Hellman Group 1 key exchange.
ubuntu: Sent DH public key, waiting for reply.
ubuntu: Received host key, type 'ssh-dss'.
ubuntu: Host '192.168.0.1' is known and matches the host key.
ubuntu: Computing shared secret key.
ubuntu: Verifying server signature.
ubuntu: Waiting for NEWKEYS message.
ubuntu: Enabling incoming encryption/MAC/compression.
ubuntu: Send NEWKEYS, enable outgoing encryption/MAC/compression.
ubuntu: Sending request for user-authentication service.
ubuntu: Service accepted: ssh-userauth.
ubuntu: Trying empty user-authentication request.
ubuntu: Authentication methods that can continue:
publickey,password,keyboard-interactive.
ubuntu: Next method to try is publickey.
ubuntu: Trying pubkey authentication with key file '/home/gui/.ssh/id_rsa'
ubuntu: Login completed, opening dummy shell channel.
ubuntu: channel 0: new [client-session]
ubuntu: Requesting channel_open for channel 0.
ubuntu: channel 0: open confirm rwindow 0 rmax 32768
ubuntu: Got channel open confirmation, requesting shell.
ubuntu: Requesting service shell on channel 0.
ubuntu: channel 1: new [client-session]
ubuntu: Requesting channel_open for channel 1.
ubuntu: Entering interactive session.
ubuntu: Sending command: wget -q http://www.google.com
ubuntu: Requesting service exec on channel 1.
ubuntu: channel 1: open confirm rwindow 0 rmax 32768
ubuntu: input_channel_request: rtype exit-status reply 0
ubuntu: channel 1: rcvd eof
ubuntu: channel 1: output open -> drain
ubuntu: channel 1: rcvd close
ubuntu: channel 1: input open -> closed
ubuntu: channel 1: close_read
ubuntu: channel 1: obuf empty
ubuntu: channel 1: output drain -> closed
ubuntu: channel 1: close_write
ubuntu: channel 1: send close
ubuntu: channel 1: full closed
ubuntu: Reading configuration data /home/gui/.ssh/config
ubuntu: Reading configuration data /etc/ssh_config
Net::SSH: Can't bind socket to port 1023: Adresse déjà utilisée at
../test_ssh.pl line 50
John Doe

2005-04-06, 12:41 pm

Bonjour

Not many answers, so...

Am Montag, 4. April 2005 21.16 schrieb gui:
> hello,
>
> I'm making a simple script that's supposed to update a certain number of
> boxes, spread around the area.
> my script is supposed to to connect to every hosts (via a file named
> "hosts") using ssh, launch wget to retrieve the patch update, and
> install it.
> Things work fine for the first host of the list. But can never get
> beyong that. I get a "Net::SSH: Can't bind socket to port 1023: Adresse
> d=E9j=E0 utilis=E9e at ./test_ssh.pl line 46" message.
>
> my question is, how can I unbind the socket used by Net::SSH::Perl ?
>
> here's part of my code:
>
> use strict;
> use Net::SSH::Perl;
>
> my ($patchurl,$stout,$sterr,$exit,$ssh);
> my @ids =3D "$ENV{HOME}/.ssh/id_rsa";
>
> my %params =3D (
> 'protocol' =3D> 2,
> 'identity_files' =3D> \@ids,
> );
>
> $patchurl =3D push @ARGV
>
> open HOSTS, "hosts" or die "je n'ai pas r=E9ussi =E0 ouvrir le fichier ho=

sts
>
> : $!";
>
> while(<HOSTS> ){
>
> (...)
>
> $ssh =3D Net::SSH::Perl->new($_, %params);
> $ssh->login("root");
> ($stout,$sterr,$exit) =3D $ssh->cmd("wget -q $patchurl");
>
> (...)
> }
>
>
> thanks


I don't see the exact reason. But after having a look in the code (Perl.pm,=
=20
sub _create_socket, which tries ports from 1023 down to 512 to bind to),=20
maybe the ssh object is not destroyed between the loops. You could try:

** define $ssh as my variable within the loop (not outside as currently)
** put the code within the loop in a separate block, containing the=20
my-Definition of $ssh
** use "undef $ssh" at the end of the loop (still within it of course)
** Insert some diagnostic code at the beginning of the loop that=20
a) examines the $ssh object before the second "loop run" and/or=20
b) sleeps for e.g. a minute, so that you can look whats happening with the=
=20
bound port (netstat -neat from cmdline)

I don't have the Modules installed, so I didn't made tests, sorry.

joe
Gui

2005-04-06, 12:41 pm

John Doe wrote:
>
>
> I don't see the exact reason. But after having a look in the code (Perl.pm,
> sub _create_socket, which tries ports from 1023 down to 512 to bind to),
> maybe the ssh object is not destroyed between the loops. You could try:
>
> ** define $ssh as my variable within the loop (not outside as currently)
> ** put the code within the loop in a separate block, containing the
> my-Definition of $ssh
> ** use "undef $ssh" at the end of the loop (still within it of course)
> ** Insert some diagnostic code at the beginning of the loop that
> a) examines the $ssh object before the second "loop run" and/or
> b) sleeps for e.g. a minute, so that you can look whats happening with the
> bound port (netstat -neat from cmdline)
>
> I don't have the Modules installed, so I didn't made tests, sorry.
>
> joe


I tried some of your recommendations, like declaring $ssh in the loop
and use "undef $ssh" at the end, but didn't solve the problem.
I did use netstat -neat during, and after the script launch. I didn't
see anything weird during the script running, I did see something
bizarre after :

tcp 0 0 192.168.0.3:1023 192.168.0.1:22
TIME_WAIT 0 0

that line, stayed there for at least a full minute before disappearing.
So I decided to add a 2min sleep at the end of the loop... and to my
surprise : IT WORKED!
I used netstat during the 2min sleep, it seems that the connection to
the 1023 socket lasts something between 1-2 minutes.

It's weird, I wish there were another way around it. If anybody got an
explanation or a better solution about this "socket bind for more than a
minute, than just disappears" enigma, I'd be glad.
Michael Gale

2005-04-06, 3:55 pm

Hello,

I am not a experienced perl programmer but from the look at your first
post with the perl code. Do you not have to run a close sessions ?

So after your commands:


$ssh = Net::SSH::Perl->new($_, %params);
$ssh->login("root");
($stout,$sterr,$exit) = $ssh->cmd("wget -q $patchurl");

#then add in
close_session($ssh);

Also, if each connection is truly a new connection, should it not be
using a different source port ? If the source port is not changing then
it looks like your code is trying to use the same SSH connection
handle.

I believe the RFC's make some reference to the tcp port being in a
TIME_WAIT state after a connection closes in case the remote machine
needs to finish up it's end and send a packet or two back.

Michael.


On Wed, 2005-04-06 at 12:52 +0200, gui wrote:
> John Doe wrote:
>
> I tried some of your recommendations, like declaring $ssh in the loop
> and use "undef $ssh" at the end, but didn't solve the problem.
> I did use netstat -neat during, and after the script launch. I didn't
> see anything weird during the script running, I did see something
> bizarre after :
>
> tcp 0 0 192.168.0.3:1023 192.168.0.1:22
> TIME_WAIT 0 0
>
> that line, stayed there for at least a full minute before disappearing.
> So I decided to add a 2min sleep at the end of the loop... and to my
> surprise : IT WORKED!
> I used netstat during the 2min sleep, it seems that the connection to
> the 1023 socket lasts something between 1-2 minutes.
>
> It's weird, I wish there were another way around it. If anybody got an
> explanation or a better solution about this "socket bind for more than a
> minute, than just disappears" enigma, I'd be glad.
>

John Doe

2005-04-06, 8:56 pm

Am Mittwoch, 6. April 2005 12.52 schrieb gui:
> John Doe wrote:
>
> I tried some of your recommendations, like declaring $ssh in the loop
> and use "undef $ssh" at the end, but didn't solve the problem.
> I did use netstat -neat during, and after the script launch. I didn't
> see anything weird during the script running, I did see something
> bizarre after :
>
> tcp 0 0 192.168.0.3:1023 192.168.0.1:22
> TIME_WAIT 0 0
>
> that line, stayed there for at least a full minute before disappearing.
> So I decided to add a 2min sleep at the end of the loop... and to my
> surprise : IT WORKED!
> I used netstat during the 2min sleep, it seems that the connection to
> the 1023 socket lasts something between 1-2 minutes.
>
> It's weird, I wish there were another way around it. If anybody got an
> explanation or a better solution about this "socket bind for more than a
> minute, than just disappears" enigma, I'd be glad.


This phenomen with the TIME_WAIT was my other thought to explain the behavi=
or=20
of your script, but then I decided not to mention it because I thought that=
=20
the sub _create_socket code in Perl.pm will handle this:

*---- code ----*
sub _create_socket {
my $ssh =3D shift;
my $sock =3D gensym;

my ($p,$end,$delta) =3D (0,1,1); # normally we use whatever port we can get
($p,$end,$delta) =3D (1023,512,-1) if $ssh->{config}->get('privilege=
d');

# allow an explicit bind address
my $addr =3D $ssh->{config}->get('bind_address');
$addr =3D inet_aton($addr) if $addr;
($p,$end,$delta) =3D (10000,65535,1) if $addr and not $p;
$addr ||=3D INADDR_ANY;

for(; $p !=3D $end; $p +=3D $delta) {
socket($sock, AF_INET, SOCK_STREAM, getprotobyname('tcp') || 0) ||
croak "Net::SSH: Can't create socket: $!";
last if not $p or bind($sock, sockaddr_in($p,$addr));
if ($! =3D~ /Address already in use/i) {
close($sock);
next;
}
croak "Net::SSH: Can't bind socket to port $p: $!";
}
if($p) {
$ssh->debug("Allocated local port $p.");
$ssh->{config}->set('localport', $p);
}

$sock;
}

*--- code ---*

The code tries to bind to several ports (1023 down to 512).

But now, when I look again - but still not deeply enough!... (and use the h=
int=20
the port 1023 was also tried in the _second_ "loop run")... the code seems=
=20
only to handle the "port already in use" case...

Please look at the above code deeper... the croak code could be the problem=
,=20
and a possibility could be not to croak so fast, but try port after port=20
until binding has been done or the port range has fully be tested.

I could have a deeper look myself tomorrow ...=E4h... apr=E8s-midi, if you =
wish=20
(we're located in the same time zone I think).

btw - where are all the cracks who have the=20
instant-shortestpossible-100%solution for all ???

greetings joe
John Doe

2005-04-07, 8:55 am

Am Dienstag, 5. April 2005 15.20 schrieb gui:
[snip]
> Net::SSH: Can't bind socket to port 1023: Adresse d=E9j=E0 utilis=E9e at
> ./test_ssh.pl line 50


There's also a mailing list dedicated to Net::SSH::Perl (from man page):

"SUPPORT
For samples/tutorials, take a look at the scripts in eg/ in the=20
distribution directory.

There is a mailing list for development discussion and usage questio=
ns. =20
Posting is limited to subscribers only. You can sign up at=20
http://lists.sourceforge.net/lists/...sftp-perl-users

Please report all bugs via rt.cpan.org at=20
https://rt.cpan.org/NoAuth/ReportBu...3Assh%3A%3Aperl
"
joe
Gui

2005-04-08, 8:56 pm

John Doe wrote:
> Am Dienstag, 5. April 2005 15.20 schrieb gui:
> [snip]
>
>
>
> There's also a mailing list dedicated to Net::SSH::Perl (from man page):
>
> "SUPPORT
> For samples/tutorials, take a look at the scripts in eg/ in the
> distribution directory.
>
> There is a mailing list for development discussion and usage questions.
> Posting is limited to subscribers only. You can sign up at
> http://lists.sourceforge.net/lists/...sftp-perl-users
>
> Please report all bugs via rt.cpan.org at
> https://rt.cpan.org/NoAuth/ReportBu...3Assh%3A%3Aperl
> "
> joe


I totally forgot about that, thanks!

I just subscribe, and send an email... I'll post the responses
Gui

2005-06-10, 8:55 pm

I'm really sorry for not replying during all this time. I'm involved in
lots of project, and I had to set this one aside for a moment. But now
I'm determined to get this thing fix!

I still have beginner level in perl, and can't quite make sense of the
code you posted.

I asked the net::ssh::perl mailing list about the pb, but the only
response I got back from that time was "use fork". unfortunatly, I don't
quite get how I should use fork (if someone knows a URL where there is
good tutorial or post or something where I can learn fork, please reply
and add that link!). Anyway, I'll send another email to the list...
maybe I'll get different responses.

I'm surprised nobody got interested in this problem. Anybody had a
socket bind problem before?

John Doe a écrit :
> Am Mittwoch, 6. April 2005 12.52 schrieb gui:
>
> This phenomen with the TIME_WAIT was my other thought to explain the behavior
> of your script, but then I decided not to mention it because I thought that
> the sub _create_socket code in Perl.pm will handle this:
>
> *---- code ----*
> sub _create_socket {
> my $ssh = shift;
> my $sock = gensym;
>
> my ($p,$end,$delta) = (0,1,1); # normally we use whatever port we can get
> ($p,$end,$delta) = (1023,512,-1) if $ssh->{config}->get('privileged');
>
> # allow an explicit bind address
> my $addr = $ssh->{config}->get('bind_address');
> $addr = inet_aton($addr) if $addr;
> ($p,$end,$delta) = (10000,65535,1) if $addr and not $p;
> $addr ||= INADDR_ANY;
>
> for(; $p != $end; $p += $delta) {
> socket($sock, AF_INET, SOCK_STREAM, getprotobyname('tcp') || 0) ||
> croak "Net::SSH: Can't create socket: $!";
> last if not $p or bind($sock, sockaddr_in($p,$addr));
> if ($! =~ /Address already in use/i) {
> close($sock);
> next;
> }
> croak "Net::SSH: Can't bind socket to port $p: $!";
> }
> if($p) {
> $ssh->debug("Allocated local port $p.");
> $ssh->{config}->set('localport', $p);
> }
>
> $sock;
> }
>
> *--- code ---*
>
> The code tries to bind to several ports (1023 down to 512).
>
> But now, when I look again - but still not deeply enough!... (and use the hint
> the port 1023 was also tried in the _second_ "loop run")... the code seems
> only to handle the "port already in use" case...
>
> Please look at the above code deeper... the croak code could be the problem,
> and a possibility could be not to croak so fast, but try port after port
> until binding has been done or the port range has fully be tested.
>
> I could have a deeper look myself tomorrow ...äh... après-midi, if you wish
> (we're located in the same time zone I think).
>
> btw - where are all the cracks who have the
> instant-shortestpossible-100%solution for all ???
>
> greetings joe

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com