Custom Linux Bash IP Network Scanner

View previous topic View next topic Go down

Custom Linux Bash IP Network Scanner

Post by jamied_uk on 11th October 2016, 00:51



Code:

#!/bin/bash
# sudo apt-get install -y fping
# Usage: ./run_all.sh "192.168.0.1/24"
#
#
myvar="$@"
RED='\033[0;31m'
GREEN='\032[0;32m'
NC='\033[0m' # No Color
echo -e "\e[92mWelcome To J~Net IP Tester 2016"
bash -x c2ip.sh "$myvar" > output.txt
clear
# & sh -x script2.sh
while read p; do
#  echo $p
# #!/bin/bash
fping -c1 -t300 $p 2>/dev/null 1>/dev/null
if [ "$?" = 0 ]
then
  #echo "Host $p ACTIVE"
  echo -e "\e[92m$p Is ACTIVE"
  #printf "Host ${NC} $p Is ${GREEN}ACTIVE\n"
else
  #echo "Host $p NOT Active"
   printf "${RED}Host $p Is NOT Active\n"
fi
done <output.txt


Last edited by jamied_uk on 11th October 2016, 01:07; edited 1 time in total
avatar
jamied_uk
Admin

Posts : 2268
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: Custom Linux Bash IP Network Scanner

Post by jamied_uk on 11th October 2016, 00:52

c2ip.sh



Code:
#!/bin/bash   
# Usage:
# ./c2ip.sh 192.168.0.1/24
#
#
#
############################
##  Methods
############################  
prefix_to_bit_netmask() {
    prefix=$1;
    shift=$(( 32 - prefix ));

    bitmask=""
    for (( i=0; i < 32; i++ )); do
        num=0
        if [ $i -lt $prefix ]; then
            num=1
        fi

        space=
        if [ $(( i % 8 )) -eq 0 ]; then
            space=" ";
        fi

        bitmask="${bitmask}${space}${num}"
    done
    echo $bitmask
}

bit_netmask_to_wildcard_netmask() {
    bitmask=$1;
    wildcard_mask=
    for octet in $bitmask; do
        wildcard_mask="${wildcard_mask} $(( 255 - 2#$octet ))"
    done
    echo $wildcard_mask;
}

check_net_boundary() {
    net=$1;
    wildcard_mask=$2;
    is_correct=1;
    for (( i = 1; i <= 4; i++ )); do
        net_octet=$(echo $net | cut -d '.' -f $i)
        mask_octet=$(echo $wildcard_mask | cut -d ' ' -f $i)
        if [ $mask_octet -gt 0 ]; then
            if [ $(( $net_octet&$mask_octet )) -ne 0 ]; then
                is_correct=0;
            fi
        fi
    done
    echo $is_correct;
}

#######################
##  MAIN
#######################
OPTIND=1;
getopts "fibh" force;

shift $((OPTIND-1))
if [ $force = 'h' ]; then
    echo ""
    echo -e "THIS SCRIPT WILL EXPAND A CIDR ADDRESS.\n\nSYNOPSIS\n  ./cidr-to-ip.sh [OPTION(only one)] [STRING/FILENAME]\nDESCRIPTION\n -h  Displays this help screen\n -f  Forces a check for network boundary when given a STRING(s)\n    -i  Will read from an Input file (no network boundary check)\n  -b  Will do the same as –i but with network boundary check\n\nEXAMPLES\n    ./cidr-to-ip.sh  192.168.0.1/24\n   ./cidr-to-ip.sh  192.168.0.1/24 10.10.0.0/28\n  ./cidr-to-ip.sh  -f 192.168.0.0/16\n    ./cidr-to-ip.sh  -i inputfile.txt\n ./cidr-to-ip.sh  -b inputfile.txt\n"
    exit
fi

if [ $force = 'i' ] || [ $force = 'b' ]; then

    old_IPS=$IPS
    IPS=$'\n'
    lines=($(cat $1)) # array
    IPS=$old_IPS
        else
            lines=$@
fi

for ip in ${lines[@]}; do
    net=$(echo $ip | cut -d '/' -f 1);
    prefix=$(echo $ip | cut -d '/' -f 2);
    do_processing=1;

    bit_netmask=$(prefix_to_bit_netmask $prefix);

    wildcard_mask=$(bit_netmask_to_wildcard_netmask "$bit_netmask");
    is_net_boundary=$(check_net_boundary $net "$wildcard_mask");

    if [ $force = 'f' ] && [ $is_net_boundary -ne 1 ] || [ $force = 'b' ] && [ $is_net_boundary -ne 1 ] ; then
        read -p "Not a network boundary! Continue anyway (y/N)? " -n 1 -r
        echo    ## move to a new line
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            do_processing=1;
        else
            do_processing=0;
        fi
    fi 

    if [ $do_processing -eq 1 ]; then
        str=
        for (( i = 1; i <= 4; i++ )); do
            range=$(echo $net | cut -d '.' -f $i)
            mask_octet=$(echo $wildcard_mask | cut -d ' ' -f $i)
            if [ $mask_octet -gt 0 ]; then
                range="{$range..$(( $range | $mask_octet ))}";
            fi
            str="${str} $range"
        done
        ips=$(echo $str | sed "s, ,\\.,g"); ## replace spaces with periods, a join...

        eval echo $ips | tr ' ' '\n'
else
exit
    fi

done
avatar
jamied_uk
Admin

Posts : 2268
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: Custom Linux Bash IP Network Scanner

Post by jamied_uk on 11th October 2016, 00:55

Code:
 Usage: ./run_all.sh "192.168.0.1/24"

More Notes!


Example 1

nmap -sL 192.168.0/24 | grep "Nmap scan report" | awk '{print $NF}'

2
nmap -sL 10.10.64.0/27 | grep "Nmap scan report" | awk '{print $NF}'

c2ip.sh 192.168.0.1/24

Black        0;30     Dark Gray     1;30
Red          0;31     Light Red     1;31
Green        0;32     Light Green   1;32
Brown/Orange 0;33     Yellow        1;33
Blue         0;34     Light Blue    1;34
Purple       0;35     Light Purple  1;35
Cyan         0;36     Light Cyan    1;36
Light Gray   0;37     White         1;37

And then use them like this in your script:

   .---------- constant part!
   vvvv vvvv-- the code from above


cat stats.txt



grep "^[[:1 received:]]" stats.txt


https://stackoverflow.com/questions/18123211/checking-host-availability-by-using-ping-in-bash-scripts

stackoverflow.com/questions/16986879/bash-script-to-list-all-ips-in-prefix
https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
https://unix.stackexchange.com/questions/148/colorizing-your-terminal-and-shell-environment
avatar
jamied_uk
Admin

Posts : 2268
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: Custom Linux Bash IP Network Scanner

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum