test if socket is open only

Discussion in 'Perl Misc' started by usaims, Mar 6, 2006.

  1. usaims

    usaims Guest

    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
     
    usaims, Mar 6, 2006
    #1
    1. Advertising

  2. usaims

    Guest

    "usaims" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Mar 6, 2006
    #2
    1. Advertising

  3. usaims

    Michael Greb Guest

    In article <>,
    "usaims" <> wrote:

    > 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";
    }

    --
    Michael
     
    Michael Greb, Mar 6, 2006
    #3
  4. usaims

    DJ Stunks Guest

    usaims wrote:
    > 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
     
    DJ Stunks, Mar 6, 2006
    #4
  5. usaims

    usaims Guest

    Thank you Mike, thats what I was looking for. Now, what would be the
    'perlish' way of doing this across 100 nodes?
     
    usaims, Mar 6, 2006
    #5
  6. usaims

    Michael Greb Guest

    In article <michael-6E8FF4.13391706032006@[192.168.2.100]>,
    Michael Greb <> wrote:
    <snip>
    > 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.
    >
    > > my $sock = IO::Socket::INET->new(PeerAddr => '10.124.251.25',
    > > PeerPort => '1234',
    > > Proto => 'tcp',
    > > ) or die "$!";
    > > print while <$sock>;


    --
    Michael
     
    Michael Greb, Mar 6, 2006
    #6
  7. usaims

    Michael Greb Guest

    In article <>,
    "usaims" <> wrote:

    > 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.

    --
    Michael
     
    Michael Greb, Mar 6, 2006
    #7
  8. usaims

    Anno Siegel Guest

    usaims <> wrote in comp.lang.perl.misc:
    > 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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Mar 7, 2006
    #8
  9. usaims

    usaims Guest

    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";
     
    usaims, Mar 7, 2006
    #9
  10. usaims

    DJ Stunks Guest

    usaims wrote:
    > 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
     
    DJ Stunks, Mar 7, 2006
    #10
  11. usaims

    Guest

    "usaims" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Mar 7, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,858
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    974
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    553
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    669
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    722
Loading...

Share This Page