1PortState(3) User Contributed Perl Documentation PortState(3)
2
3
4
6 IO::Socket::PortState - Perl extension for checking the open or closed
7 status of a port.
8
10 use strict;
11 use warnings;
12 use IO::Socket::PortState qw(check_ports);
13
14 my %porthash = ( ... );
15
16 check_ports($host,$timeout,\%porthash);
17
18 for my $proto (keys %porthash) {
19 for(keys %{ $porthash{$proto} }) {
20 print "$proto $_ is not open ($porthash{$proto}->{$_}->{name}) if !$porthash{$proto}->{$_}->{open};
21 }
22 }
23
25 You can use it to check if a port is open or closed for a given host
26 and protocol.
27
28 EXPORT
29 None by default. But you can export check_ports();
30
32 This function tests your \%porthash and sets a protocol/port's open and
33 note keys (see \%porthash below for details).
34
35 By default it determines if "open" is 1 or 0 if the IO::Socket::INET
36 object is defined or not. For protocols not supported by
37 IO::Socket::INET or for custom tests (IE open just to specific hosts,
38 closed because activley blocked, service is down, etc) use \&handler
39 (see \&handler below)
40
41 check_ports($host,$timeout,\%porthash,\&handler);
42
43 Called in void contect it modifies the hashref given. Otherwise it
44 returns a new hash ref which is usefull for looping through the same
45 \%porthash for multiple hosts:
46
47 my %porthash = ( ... );
48 for(@hosts) {
49 my $host_hr = check_ports($_,$timeout,\%porthash);
50 print "Report for $_\n";
51 # do something with $host_hr
52 }
53
54 vs void context:
55
56 my %porthash = ( ... );
57 check_ports($host,$timeout,\%porthash);
58 # now %porthash has been directly changed
59
60 \%porthash
61 This hash is a bit complex by necessity. (but its not so bad ;p)
62
63 The keys are the protocol (tcp, udp, ...) as can be used by
64 IO::Socket::INET->new()'s "Proto" option (or whatever is valid for your
65 custom \&handler
66
67 The values are a hashref. In this hashref the keys are the numeric
68 ports and the valuse are a hashref.
69
70 This hashref has only one key "name" whose value can be an arbitrary
71 label for your use and once run it sets "open" to 1 or 0 and "note" to
72 "builtin()" so you knwo how "open" was figured.
73
74 my %check = (
75 tcp => {
76 80 => {
77 name => 'Apache',
78 },
79 443 => {
80 name => 'SSL Apache',
81 },
82 },
83 udp => {
84 53 => {
85 name => 'DNS'
86 },
87 465 => {
88 name => 'smtp tls/ssl'
89 },
90 },
91 );
92
93 \&handler
94 Here is an example handler function you can use as a road map:
95
96 sub handler {
97 my($ent_hr,$host,$port,$proto,$timeout) = @_;
98
99 # use $host, $port, $protocol, and $timeout to determine what you want however you like here
100
101 # at a minimum do these two:
102 $ent_hr->{open} = ???; # do what you like to set its open status to 1 or 0
103 $ent_hr->{note} = 'my handler()';
104
105 # set any other info you wanted here also...
106 if(!$ent_hr->{open}) {
107 $ent_hr->{closed_reason} = ???; # do what you like to set details about why its not open (blocked, not running, etc)
108 }
109 }
110
112 This module's life came around as a result of wanting to monitor
113 specific ports on several servers, specifically servers running cPanel
114 (<http://cpanel.net/>). To make it easier to do that and provide a
115 model to make it easier for anyone to create a module that is "server
116 specific" I've created IO::Socket::PortState::cPanel
117
118 If you want to do the same thing please use it as a guide, all you
119 would need to do is change the hashrefs and package specific info and
120 voila its all set :)
121
122 If you do use IO::Socket::PortState::cPanel as a model (and I hope you
123 do so that using any IO::Socket::PortState::* module will have a
124 specific consistent use) please reference it in the POD of your module
125 as outlined in the POD of IO::Socket::PortState::cPanel.
126
128 Daniel Muey, <http://drmuey.com/cpan_contact.pl>
129
131 Copyright 2005 by Daniel Muey
132
133 This library is free software; you can redistribute it and/or modify it
134 under the same terms as Perl itself.
135
136
137
138perl v5.32.0 2020-07-28 PortState(3)