103k views
3 votes
Using Python, you are asked to write a script which will perform the following tasks:

A. Retrieve the IP address of the host you are using.
B. Discover which hosts are alive/active on your IP address range.
C. Show how many hosts are alive/active on your IP address range.
D. If alive/active hosts are discovered, which ports are open on those hosts.
E. Provide an in-text discussion/description of the above exercises

User DanV
by
8.1k points

1 Answer

4 votes

Final answer:

To perform the tasks using Python, you can retrieve the IP address of the host, discover which hosts are alive/active, show how many hosts are alive/active, and find out which ports are open on those hosts using the nmap and socket libraries.

Step-by-step explanation:

A. To retrieve the IP address of the host you are using in Python, you can use the socket library. Here's an example:

import socket

hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)
print(f'The IP address is: {ip_address}')

B. To discover which hosts are alive/active on your IP address range, you can use the nmap library in Python. Here's an example:

import nmap

nm = nmap.PortScanner()
range_of_ips = '192.168.1.1-10'
nm.scan(hosts=range_of_ips, arguments='-sn')

for host in nm.all_hosts():
print(f'Host: {host} is alive')

C. To show how many hosts are alive/active on your IP address range, you can simply count the number of hosts that were returned in the previous step. For example:

print(f'There are {len(nm.all_hosts())} hosts alive')

D. To find out which ports are open on the alive/active hosts, you can use the nmap library again. Here's an example:

for host in nm.all_hosts():
for proto in nm[host].all_protocols():
lport = nm[host][proto].keys()
for port in lport:
print(f'Host: {host} Protocol: {proto} Port: {port} is open')

E. In this script, we have performed various tasks related to IP addresses and network scanning using Python. We retrieved the IP address of the host, discovered the alive/active hosts on a given IP address range, showed how many hosts were alive/active, and found out which ports were open on those hosts. These tasks can be useful in networking and security-related scenarios.

User Mukundhan
by
7.8k points