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.