| 1 | package MYDan::Project::Check::Port; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | use IO::Socket::INET; | ||
| 6 | |||
| 7 | # port: 8080 | ||
| 8 | # type: tcp # type: udp default | ||
| 9 | # host: abc.foo.org # host: 127.0.0.1 default | ||
| 10 | |||
| 11 | sub new | ||
| 12 | { | ||
| 13 | my ( $class, %self ) = @_; | ||
| 14 | confess "port undef.\n" unless defined $self{port}; | ||
| 15 | |||
| 16 | $self{host} ||= '127.0.0.1'; | ||
| 17 | $self{type} ||= 'tcp'; | ||
| 18 | |||
| 19 | bless \%self, ref $class || $class; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub check | ||
| 23 | { | ||
| 24 | my $this = shift; | ||
| 25 | my ( $port, $type, $host ) = @$this{qw( port type host )}; | ||
| 26 | |||
| 27 | IO::Socket::INET->new( | ||
| 28 | PeerAddr => "$host:$port", Blocking => 0, | ||
| 29 | Timeout => 10, Type => SOCK_STREAM, | ||
| 30 | Proto => $type | ||
| 31 | ) | ||
| 32 | ? print "$type => $host:$port :OK\n" | ||
| 33 | : die "$type => $host:$port :FAIL\n"; | ||
| 34 | |||
| 35 | } | ||
| 36 | |||
| 37 | 1; | ||
| 38 | __END__ |