#!/user/bin/env python3
#---------------------------------------------------------------------#
# Script Name                Ops Challenge 12
# Author                     Kimberley Cabrera-Boggs
# Date of last revision      October 20, 2020
# Description of purpose     Network Security Tool w/Scapy Part 2 of 3
#---------------------------------------------------------------------#
#   ,-~~-.___.
#  / |  '     \
# (  )         0
#  \_/-, ,----'
#     ====           //
#    /  \-'~;    /~~~(O)
#   /  __/~|   /       |
# =(  _____| (_________|

# Part 2 of 3

from ipaddress import IPv4Network
import random
from scapy.all import ICMP, IP, sr1, TCP

# IP range to ping
network = "192.168.117.0/24"

# Addresses 
addresses = IPv4Network(network)
live_count = 0

# ICMP ping request, get response host address
    if (host in (addresses.network_address, addresses.broadcast_address)):
        # Ignore network and broadcast addresses

    resp = sr1(
        IP(dst=str(host))/ICMP(),
        timeout=2,
        verbose=0,
    )

    if resp is None:
        print(f"{host} is down or unresponsive.")
    elif (
        int(resp.getlayer(ICMP).type)==3 and
        int(resp.getlayer(ICMP).code) in [1,2,3,9,10,13]
    ):
        print(f"{host} is actively blocking ICMP.")
    else:
        print(f"{host} is actively responding.")
        live_count += 1

print(f"{live_count}/{addresses.num_addresses} the hosts are online.")
