portping - perl script

2007. 8. 31. 13:59
반응형
#!/usr/bin/perl
#
# portping - ping a port on a server, ssh by default.
#
# This program works by trying to establish a TCP connection, no ICMP
#  is used. This can be useful in senarios where everything but ssh
#  has been blocked by firewalls.
#
# 25-Mar-2004, ver 0.60         (check for newer versions)
#
#
# USAGE: portping [-h] | hostname [port]
#    eg,
#        portping mars          # try TCP port 22 (ssh) on mars
#        portping mars 21       # try TCP port 21 on mars
#
# There are currently no advanced options for port scanning or different
#  TCP flags, as there are many existing freeware tools to do this.
#  portping is designed to be a simple handy perl script.
#
#
# SEE ALSO: nmap (port scanner), hping2 (super ping)
#
# COPYRIGHT: Copyright (c) 2004 Brendan Gregg.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#  (
http://www.gnu.org/copyleft/gpl.html)
#
# 25-Mar-2004   Brendan Gregg   Created this.

use IO::Socket;


#
#  Command line arguments
#
&Usage() if ($ARGV[0] eq "-h" || $ARGV[0] eq "--help" || @ARGV == 0);
$host = $ARGV[0];
if (defined $ARGV[1]) {
        $port = $ARGV[1];
} else {
        $port = 22;
}

#
#  Try to connect
#
$remote = IO::Socket::INET->new(
        Proto    => "tcp",
        PeerAddr => "$host",
        PeerPort => "$port",
        Timeout  => 8
);

#
#  Print response
#
if ($remote) {
        print "$host is alive\n";
        close $remote;
        exit(0);
} else {
        print "$host failed\n";
        exit(1);
}


# Usage - print usage message and exit
#
sub Usage {
        print STDERR "USAGE: $0 [-h] | hostname [port]\n";
        print STDERR "   eg, $0 mars        # try port 22 (ssh) on mars\n";
        print STDERR "       $0 mars 21     # try port 21 on mars\n";
        exit 1;
}
반응형

'alt.comp > scripts' 카테고리의 다른 글

perl debugging  (0) 2007.08.31

+ Recent posts