#!/bin/bash

#
# $Id: psw,v 1.2 2008/09/10 10:09:22 raptor Exp $
#
# psw v0.1 - Simple C class ping sweeper
# Copyright (c) 2002 Raptor <raptor@0xdeadbeef.eu.org>
#
# PSW is a very basic ping sweeper written in bash.
# Useful for the fast identification of active hosts
# in a big network, for security evaluation tasks.
#
# Usage example: ./psw 192.168.0 1 254
#

# Some vars
log=psw.log
ping=/bin/ping

# Command line
network=$1
begin=$2
end=$3
count=0

# Local functions
function green() {
        echo -e "\033[01;32m$@\033[00m"  
}

function yellow() {
        echo -e "\033[01;33m$@\033[00m"
}

function red() {
        echo -e "\033[01;31m$@\033[00m"
}

function usage() {
	echo ""
	echo "PSW 0.1. Simple C class ping sweeper"
	echo "By Raptor <raptor@0xdeadbeef.eu.org>"
	echo ""
	echo "usage  : ./psw <network> <begin> <end>"
	echo "example: ./psw 192.168.0 1 254"
	echo ""
	exit 1
}

# Input control
if [ -z "$3"  ]; then
	usage
fi

# Interactive logging
echo ""
echo "PSW 0.1. Simple C class ping sweeper"
echo "By Raptor <raptor@0xdeadbeef.eu.org>"
echo ""
yellow "[x] Starting with: ${network}.${begin}"
echo ""

# Perform the ping sweep
while :
do
        $ping -c 1 -w 5 $network.$begin 1>/dev/null 2>/dev/null

# Check ping return value
	if [ $? -eq 0 ]; then
		green "${network}.${begin}"
		echo "${network}.${begin}" >> $log
		count=$(($count + 1))
	else
		echo "${network}.${begin}"
	fi

# Go for the next address
        if [ $begin -eq $end ]; then
                break
        else
		begin=$(($begin + 1))
        fi

done

echo ""
yellow "[x] Finished with: ${network}.${end}"
red    "[x] Final results: ${count} hosts found"
echo ""

exit 0
