test if socket is open only

U

usaims

Hello,

I need to test if a remote server has a port open. I do not want to
write to it (the developer won't let me write to it), just verify if it
is open. The script below kind of does what I need but it hangs after I
execute it. Can sombody suggest a better solution?

#!/usr/bin/perl -w
use strict;
use IO::Socket::INET;


my $sock = IO::Socket::INET->new(PeerAddr => '10.124.251.25',
PeerPort => '1234',
Proto => 'tcp',
) or die "$!";
print while <$sock>;

usaims
 
X

xhoster

usaims said:
Hello,

I need to test if a remote server has a port open. I do not want to
write to it (the developer won't let me write to it),

Aren't you the developer?
just verify if it
is open. The script below kind of does what I need but it hangs after I
execute it. Can sombody suggest a better solution?

#!/usr/bin/perl -w
use strict;
use IO::Socket::INET;

my $sock = IO::Socket::INET->new(PeerAddr => '10.124.251.25',
PeerPort => '1234',
Proto => 'tcp',
) or die "$!";
print while <$sock>;

I guess I'd start by getting rid of the part of the script that hangs,
which is probably the last line.

Ohterwise, maybe you need to do something "alarm"ing

Xho
 
M

Michael Greb

usaims said:
Hello,

I need to test if a remote server has a port open. I do not want to
write to it (the developer won't let me write to it), just verify if it
is open. The script below kind of does what I need but it hangs after I
execute it. Can sombody suggest a better solution?

#!/usr/bin/perl -w

Better to "use warnings;" rather then the -w on the shebang line.
use strict;
use IO::Socket::INET;

You really need the print and that is what is causing your script to
hang. The following line will fail if the port is not open so you just
need to check the return code.
my $sock = IO::Socket::INET->new(PeerAddr => '10.124.251.25',
PeerPort => '1234',
Proto => 'tcp',
) or die "$!";
print while <$sock>;

usaims

You could rewrite this as:

#!/usr/bin/perl

use strict;
use warnings;
use IO::Socket::INET;


if (IO::Socket::INET->new(PeerAddr => '10.124.251.25',
PeerPort => '1234',
Proto => 'tcp',
)
) {
print "Port open.\n";
}
else {
print "Port closed.\n";
}
 
D

DJ Stunks

usaims said:
Hello,

I need to test if a remote server has a port open. I do not want to
write to it (the developer won't let me write to it), just verify if it
is open. The script below kind of does what I need but it hangs after I
execute it. Can sombody suggest a better solution?

#!/usr/bin/perl -w
use strict;
use IO::Socket::INET;


my $sock = IO::Socket::INET->new(PeerAddr => '10.124.251.25',
PeerPort => '1234',
Proto => 'tcp',
) or die "$!";
print while <$sock>;

it's not 'hung', it's waiting for information on the socket so it can
print it out as per your instructions.

-jp
 
U

usaims

Thank you Mike, thats what I was looking for. Now, what would be the
'perlish' way of doing this across 100 nodes?
 
M

Michael Greb

Michael Greb said:
You really need the print and that is what is causing your script to ^ don't
hang. The following line will fail if the port is not open so you just
need to check the return code.
 
M

Michael Greb

usaims said:
Thank you Mike, thats what I was looking for. Now, what would be the
'perlish' way of doing this across 100 nodes?

You need to quote some context of the message you are replying like I
have done. Check the group posting guidelines for instructions on doing
this via Google Groups.

How do you have the list of IPs stored? If you have an array your
could use a for loop but I'm not sure that sample code should be used in
such a case. I imagine it would leave all of the connections open until
the script was finished. Perhaps scoping the IO::Socket::INET to the
for loop would cause the object to get destroyed right away closing the
connection. There are likely much better choices for doing the actual
verification of the port being open but I haven't done sockets stuff in
perl for some time.
 
A

Anno Siegel

usaims said:
Thank you Mike, thats what I was looking for. Now, what would be the
'perlish' way of doing this across 100 nodes?

How about you make an attempt and show us what you've done?

Anno
 
U

usaims

Here's my quick way of doing it across 80 servers, I'm guessing its
fine because I'm getting results, sorry, not familiar with sockets:

#!/usr/bin/perl
use strict;
use warnings;

use IO::Socket::INET;
my $host = '10.170.130.';
for (my $x = 1; $x <= 80; $x++) {


if (IO::Socket::INET->new(PeerAddr => "$host$x",
PeerPort => '9866',
Proto => 'tcp',
)
) {
print "$host$x; Port open.\n";


}


else {
print "$host$x;Port closed.\n";
 
D

DJ Stunks

usaims said:
Here's my quick way of doing it across 80 servers, I'm guessing its
fine because I'm getting results, sorry, not familiar with sockets:

seems fine to me, some comments:
#!/usr/bin/perl
use strict;
use warnings;

use IO::Socket::INET;
my $host = '10.170.130.';
for (my $x = 1; $x <= 80; $x++) {

C-style for loops should only be used very rarely in Perl.

for my $x (1..80) {

my $host_ip = $host . $x;
if (IO::Socket::INET->new(PeerAddr => "$host$x",
PeerPort => '9866',
Proto => 'tcp',
)
) {
print "$host$x; Port open.\n";
}
else {
print "$host$x;Port closed.\n";
}

I would factor out common code, but it's a minor gripe:

print "$host_ip : Port ";
if ( IO::Socket::INET->new(PeerAddr => $host_ip, <etc>) {
print "open\n";
}
else {
print "closed\n";
}

-jp
 
X

xhoster

usaims said:
Here's my quick way of doing it across 80 servers, I'm guessing its
fine because I'm getting results, sorry, not familiar with sockets:

#!/usr/bin/perl
use strict;
use warnings;

use IO::Socket::INET;
my $host = '10.170.130.';
for (my $x = 1; $x <= 80; $x++) {

foreach my $x(1..80) {
if (IO::Socket::INET->new(PeerAddr => "$host$x",
PeerPort => '9866',
Proto => 'tcp',
)

It is is possible (but AFAICT, unusual) for the INET->new itself to block
for long periods, so that you may need to use nonblocking connects (ala
LWP/Parallel/Protocol/http.pm) or timeouts or forking multi/threading
server.

But I would not worry about that until it proves itself to be an issue.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top