#!/usr/bin/perl
#
# Copyright (C) 2005-2006 Joshua D. Abraham (jabra@ccs.neu.edu)
#
# This program is released under the terms of the GNU General Public License
# (GPL), which is distributed with this software in the file "COPYING".
# The GPL specifies the terms under which users may copy and use this software.
#
# PBNJ 2.0
# (P)orts (B)anners N' (J)unk
#
# Author:   Joshua D. Abraham
# Date:     March 15, 2006
# Updated:  November 15, 2006
# Version:  2.04
# 
#
# Genlist - ping scanner
#

use strict;
use warnings;

use FileHandle;
use Nmap::Parser;
use Getopt::Long;
use File::Which;

use vars qw( $PROG );
( $PROG = $0 ) =~ s/^.*[\/\\]//;    # Truncate calling path from the prog name

my $AUTH    = 'Joshua D. Abraham';  # author
my $VERSION = '2.04';               # version

my $np = new Nmap::Parser;          # parser object

my $list;                           # output list of live hosts;
my @output_files;
my $ipfound;
my $type      = 'help';
my $outputdir = '.';                # output database directory
my $nmapPath  = which('nmap');      # nmap path
my $args      = "-sP";
my $interface = "";                 # default interface to perform scan
my @ipRange;
my %options;                        # getopts hash
##############################################################################
# scan ->
# performs a ping scan and prints the list of machines that are up
# side effects: exits program
##############################################################################
sub scan {
    $np->parsescan( $nmapPath, $args, @ipRange );
    for my $host ( $np->get_ips('up') ) {
        print "$host\n";
    }
    exit;
}
##############################################################################
#
# help ->
# display help information
# side effect:  exits program
#
##############################################################################
sub help {
    print "Usage: $PROG [Input Type] [General Options]
Input Type:
   -s  --scan <target>      Ping Target Range ex: 10.0.0.\\*

Scan Options:
   -n  --nmap <path>        Path to Nmap executable 
       --inter <interface>  Perform Nmap Scan using non default interface

General Options:
   -v  --version            Display version
   -h  --help               Display this information

Send Comments to Joshua D. Abraham ( jabra\@ccs.neu.edu )\n";
    exit;
}

sub print_version {
    print "$PROG version $VERSION by $AUTH\n";
    exit;
}

##############################################################################

GetOptions(
    \%options,
    'scan|s=s', 'nmap|n=s', 'inter=s',
    'help|h'    => sub { help(); },
    'version|v' => sub { print_version(); },
    )
    or exit 1;

if ( $options{'nmap'} ) {
    if ( -X $options{'nmap'} ) {
        $nmapPath = $options{'nmap'};
    }
    else {
        print $options{'nmap'} . " isn't executable using $nmapPath\n";
    }
}
if ( $options{'scan'} ) {
    my $ipRange;
    if ( $options{'scan'}
        =~ /(\d{1,3})\.(\d{1,3}|\*)\.(\d{1,3}|\*)\.(0\/\d{1,2}|\d{1,3}|\*|'*')/
        )
    {
        $ipRange = $options{'scan'};
        $type    = 'scan';
    }
    else {
        print "scan is $options{'scan'}";
    }
    push( @ipRange, $ipRange );
}
if ( $options{'inter'} ) {
    $interface = "-e " . $options{'interface'} . " ";
    $args      = $args . $interface;
}

# make sure something is passed
help() if ( $type ne 'scan' );
scan();
