PortState(3pm)
πΈ NAME πΈ
IO::Socket::PortState β bestie, it's a Perl extension for checking whether a port is open or closed. That's the whole vibe. π
β¨ SYNOPSIS β¨
okay so here's the whole thing in action, no cap:
use strict;
use warnings;
use IO::Socket::PortState qw(check_ports);
my %porthash = ( ... );
check_ports($host,$timeout,\%porthash);
for my $proto (keys %porthash) {
for(keys %{ $porthash{$proto} }) {
print "$proto $_ is not open ($porthash{$proto}->{$_}->{name}) if !$porthash{$proto}->{$_}->{open};
}
}
π¦ DESCRIPTION π¦
You can use it to check if a port is open or closed for a given host and protocol. That's it. That's the job. π«Ά
π EXPORT π
None by default. But you can export check_ports(); if you want her on the guest list.
π check_ports() π
This function tests your \%porthash and sets a protocol/port's open and note keys (peep \%porthash below for the deets, iykyk).
By default it figures out whether open is 1 or 0 based on whether the IO::Socket::INET object is defined or not. For protocols that IO::Socket::INET doesn't support, or for custom tests (likeβ¦ open just to specific hosts, closed because it's activley blocked, service is down, etc) β use \&handler (scroll down for \&handler bestie):
check_ports($host,$timeout,\%porthash,\&handler);
Called in void context it modifies the hashref you gave it. Otherwise it returns a new hash ref, which is lowkey usefull for looping through the same \%porthash for multiple hosts:
my %porthash = ( ... );
for(@hosts) {
my $host_hr = check_ports($_,$timeout,\%porthash);
print "Report for $_\n";
# do something with $host_hr
}
vs void context, which is giving in-place makeover:
my %porthash = ( ... );
check_ports($host,$timeout,\%porthash);
# now %porthash has been directly changed
π %porthash π
This hash is a bit complex by necessity. (but its not so bad ;p)
The keys are the protocol (tcp, udp, ...) as can be used by IO::Socket::INET->new()'s Proto option (or whatever is valid for your custom \&handler).
The values are a hashref. Inside that hashref, the keys are the numeric ports and the valuse are a hashref. (nested hashrefs, she's layered π )
This hashref has only one key, name, whose value can be an arbitrary label for your use β and once it's run, it sets open to 1 or 0 and note to "builtin()" so you knwo how open was figured out. π΅οΈβοΈ
my %check = (
tcp => {
80 => {
name => 'Apache',
},
443 => {
name => 'SSL Apache',
},
},
udp => {
53 => {
name => 'DNS'
},
465 => {
name => 'smtp tls/ssl'
},
},
);
π§ &handler π§
Here's an example handler function you can use as a road map, bestie:
sub handler {
my($ent_hr,$host,$port,$proto,$timeout) = @_;
# use $host, $port, $protocol, and $timeout to determine what you want however you like here
# at a minimum do these two:
$ent_hr->{open} = ???; # do what you like to set its open status to 1 or 0
$ent_hr->{note} = 'my handler()';
# set any other info you wanted here also...
if(!$ent_hr->{open}) {
$ent_hr->{closed_reason} = ???; # do what you like to set details about why its not open (blocked, not running, etc)
}
}
π HOW TO EXPAND ON IO::Socket::PortState π
This module's whole origin story? It came around because someone wanted to monitor specific ports on several servers β specifically servers running cPanel (<http://cpanel.net/>). To make that easier, and to hand everyone a model for building a "server specific" module, IO::Socket::PortState::cPanel was created. iconic behavior fr.
If you want to do the same thing, please use it as a guide β all you'd need to do is change the hashrefs and package specific info and voila, its all set :) β¨
If you do use IO::Socket::PortState::cPanel as a model (and honestly, hope you do, so that using any IO::Socket::PortState::* module will have a specific consistent use), please reference it in the POD of your module as outlined in the POD of IO::Socket::PortState::cPanel.
π AUTHOR π
Daniel Muey, <http://drmuey.com/cpan_contact.pl>
π COPYRIGHT AND LICENSE π
Copyright 2005 by Daniel Muey
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page bestie π this one shipped with perl v5.34.0, dated 2022-06-16, living in the PortState(3pm) slot of your manual. slay, go check some ports π
β¨
π¦ where this page comes from
- debian bookworm: libio-socket-portstate-perl 0.03-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libio-socket-portstate-perl 0.03-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libio-socket-portstate-perl 0.03-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms