Network scanner use case.

Another simple example of Python usage is network scanner tool which will scan computers within the network and provide results to the user that can be used for network administration and ethical hacking purposes.

Network scanner with Python.

Python code to implement Network scanner:

The code itself can be executed from command line specifying file name and IP address
- # python3 --i .

import scapy.all as scapy
import optparse

#function to deal with user input
def get_user_input():
    parse_object = optparse.OptionParser()
    parse_object.add_option("-i","--ipaddress", dest="ip_address",help="Enter IP Address")

    (user_input,arguments) = parse_object.parse_args()

    if not user_input.ip_address:
        print("Enter IP Address")

    return user_input

#function to make arp request, broadcasting it and dealing with response
def scan_my_network(ip):
    arp_request_packet = scapy.ARP(pdst=ip)
    broadcast_packet = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
    combined_packet = broadcast_packet/arp_request_packet
    (answered_list,unanswered_list) = scapy.srp(combined_packet,timeout=1)

user_ip_address = get_user_input()

See also related topics: