Thread Programmstatus über Port ermitteln (9 answers)
Opened by DonKnilch at 2009-11-27 12:54

Linuxer
 2009-11-27 13:28
#128627 #128627
User since
2006-01-27
3890 Artikel
HausmeisterIn

user image
Ich hatte mir mal ein Skript geschrieben, um Ports auf Erreichbarkeit zu testen (hatte von telnet $ip $port die Nase voll).

Rausgekommen ist folgendes Programm:
(War ein Schnellschuß, hat noch Optimierungsbedarf ;) )
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/perl
# vi: set ts=4 sw=4 sts=2 et:
use strict;
use warnings;
use IO::Socket;
use File::Basename qw( basename );

# Author:   linuxer <winfree@gmx.de>
# Date:      22. Juli 2008

# Description:
#   checks if remote host listens to specified port(s)


#> global variables (the less you use, the better the script)
#> --------------------------------------------------------------------------

our $VERSION = '0.34';


#> sub routines
#> --------------------------------------------------------------------------

{
    my $failed = 0;

    sub checkport {
        my $host = shift;
        my $port = shift;

        my $socket = IO::Socket::INET->new(
            PeerAddr => $host,
            PeerPort => $port,
            Protocol => q{tcp},
            Timeout  => 2,
        );

        if ( $socket ) {
            close $socket;
            return qq{+ $host tcp/$port reached\n};
        }
        else {
            $failed++;
            return qq{- $host tcp/$port NOT reached: $!\n};
        }
    }
    #

    sub grand_final {
        print qq{$failed not reached\n} if ( $failed );
        exit $failed;
    }
    #

}

sub print_help {
    my $program = basename $0;

    print <<"HELP";

SYNOPSIS

    $program  remote_host tcp_port [tcp_port ...]

DESCRIPTION

This script simply tries to open a connection to the remote host and the
specified port(s). It is meant as a simple check, whether a remote host
listens to a specific port (and is reachable).

In case the specified port can't be reached, the script generates an
error message with a reason. This reason is taken from the system itself.
Depending on the system, this reason may be usable (linux; connection
refused / timed out) or not (windows; connect failed).

EXAMPLE

    $program localhost 22

checks if localhost is reachable on tcp port 22.

HELP

}
#


#> main script
#> --------------------------------------------------------------------------


my $remote = shift @ARGV;

if ( !$remote or $remote =~ m/\A-/ or ! @ARGV ) {
    print_help();
    exit 0;
}
#

for my $port ( @ARGV ) {
    print checkport( $remote, $port ) if $port =~ m/\A \d+ \z/x;
}

grand_final();

__END__

checkport.pl
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread Programmstatus über Port ermitteln