My first module, well - a first draft anyway - I have needed this functionality on several different occasions and decided to wrap it up into a module.


UPDATE
Updated to include some fixes suggested below. Thanks all!
use strict; use warnings; package mship; use IO::Socket::INET; use Socket; =head1 NAME mship - The mothership connection or how I learned to stop worrying and trust the script. =head1 VERSION Version 0.03 =cut our $VERSION = '0.03'; =head1 SYNOPSIS use mship; my %status=mship::connection('IP', 'TCP_PORT'); my $status=mship::mstat('IP', 'TCP_PORT') ? "Up" : "Down"; ... =head1 FUNCTIONS =head2 connection =cut ############## sub connection ############## { my $IP = shift; my $TCP_PORT = shift; my ( $ipadd, $port_name, $remote_name ); $ipadd = Socket::inet_aton($IP) or die "$IP is not valid\n"; $port_name = getservbyport($TCP_PORT, 'tcp') or $port_name = "unknown"; $remote_name = gethostbyaddr( $ipadd, 2 ) or die "Unable to resolve hostname for $IP\n"; my %stats = ( 'IP' => $IP, 'PORT' => $TCP_PORT, 'port_name' => $port_name, 'remote_name' => $remote_name, 'status' => &mstat($IP, $TCP_PORT) ); print "sub connection port = $TCP_PORT IP = $IP\n"; return %stats or die "unable to return the value of \%stats\n";; } =head2 mstat =cut ############# sub mstat ############# { my ( $ip, $tcp_port ) = @_; my $sock = IO::Socket::INET ->new( Timeout => 5, PeerAddr => $ip, PeerPort => $tcp_port, Proto => 'tcp' ); return $sock ? 1 : 0; } =head1 AUTHOR Ted Fiedler, C<< <fiedlert@gmail.com> >> =head1 BUGS Please report any bugs or feature requests to C<fiedlert@gmail.com> =head1 ACKNOWLEDGEMENTS =head1 COPYRIGHT & LICENSE Copyright 2003, 2004, 2005 Ted Fiedler, All Rights Reserved. This program is free software; you can redistribute it and/or modify i +t under the same terms as Perl itself. =cut 1; # End of mship

Replies are listed 'Best First'.
Re: mship.pm
by 5mi11er (Deacon) on Jun 17, 2005 at 22:18 UTC
    Ok, I've waited several hours for somebody else to comment first. But, alas, no takers.

    I'm sure it was perfectly obvious to you why you've created a module to do whatever it is this does, but, it would have been nice to hear your story. And even now that I've spent the time to read through it and attempt to figure out what it does, I'm still clueless as to what "mship" means or stands for.

    For others who don't want to spend time reading the code, my understanding of it is to

    • A) attempt a connection to a server/port, returning various information including whether the connection succeeded (up) or failed (down)
    • B) and includes an easy way to get the status of said connection.

    I further assume this is a portion for some sort of server monitoring utility...

    -Scott

      I'm still clueless as to what "mship" means or stands for.
      I couldn't think of a name for it and I just happened to be listening to George Clinton, Mothership Connection. Im lazy, so Mothership turned into mship.

      I further assume this is a portion for some sort of server monitoring utility...
      Yes - its part of a server monitoring utility...

      Ted
      --
      "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
        --Ralph Waldo Emerson
        So, Ted, you've received lots of feedback on how to improve the code, yet you're thus far silent in both threads as to whether you've accepted the feedback, and if you have accepted the comments, you should probably update the original "snippet" above...

        The Critical comments made thus far are, in this case, not personal attacks. Silence from you makes it seem like you're not interested in the feedback you've received.

        Simply posting random code without a story as to what the code does, and why someone else might find it valuable means you've relagated such code to the bottom of the dustbin of snippets. You cared enough to post it originally, don't let a few critical comments make you abandon your case, fix it, make it better, make it usable to others, and state why you think it should be used by others.

        -Scott

Re: mship.pm
by ikegami (Patriarch) on Jun 17, 2005 at 22:56 UTC

    This module was discussed in another thread. I know of three problems:

    1) This module polutes the first user's namespace with 161 symbols (3 from Carp, 158 from Socket 0 from IO::Socket::INET) because the use statements comes before package statement.

    2) This module has a method called stat. Be careful not to override the builtin stat with the one from this module.

    3) This module's stat does not return a boolean value. You must string-compare the return value with "Up" to determine if the the connection can be established.