#!/bin/sh
# -*- perl -*-
# This code allows us to start perl from our path or an environment variable
# rather than hardcoding a path into the #! line.  It works from sh or csh.
(exit $?0) && eval 'exec ${QPERLQ-perl} -x $0 ${1+"$@"}'
if (! $?QPERLQ) setenv QPERLQ perl
exec $QPERLQ -x $0 $argv:q

#!/usr/local/bin/perl -w
#
# $Id: ipscan,v 1.4 1998/05/10 14:29:51 ejb Exp $
# $Source: /home/ejb/scripts/RCS/ipscan,v $
# $Author: ejb $
#


$whoami = ($0 =~ m,([^/]*)$,) ? $1 : $0;

$max = 1024;
if (@ARGV == 2)
{
    $max = pop(@ARGV);
}

use Socket;

&usage unless @ARGV == 1;
$them = shift(@ARGV);

$sockaddr = 'S n a4 x8';
chop($hostname = `hostname`);

($x, $x, $proto) = getprotobyname('tcp');
($x, $x, $x, $x, $thisaddr) = gethostbyname($hostname);
($x, $x, $x, $x, $thataddr) = gethostbyname($them);

$this = pack($sockaddr, &AF_INET, 0, $thisaddr);

$i = 0;
sub quit_handler
{
    print "$whoami: checked up to port $i\n";
    $SIG{'QUIT'} = \&quit_handler;
}
$SIG{'QUIT'} = \&quit_handler;
print "Starting.  Send SIGQUIT (usually ctrl-\\) to see progress.\n";
for ($i = 0; $i < $max; $i++)
{
    print "$whoami: established tcp connection to $them on port $i\n"
	if &try_port($i);
}

sub try_port
{
    local($port) = shift;
    local($result);
   
    $that = pack($sockaddr, &AF_INET, $port, $thataddr);
    socket(S, &PF_INET, &SOCK_STREAM, $proto) || die "socket: $!\n";
    eval
    {
	bind(S, $this) || die "bind: $!\n";
	$result = (connect(S, $that) ? 1 : 0);
    };
    warn "port $port: $@" if $@;
    close(S);
    $result;
}


sub usage
{
    print STDERR "Usage: $whoami host [max_port]\n";
    exit 2;
}
