Here, we will discuss some of the modules that are used for networking. Using such modules, we can check the reachability of the device, gather information about the device, find out open and close ports to the target.
We will start by using the pythonping module. This is a simple module that can be used to check the ping reachability of the device. Script using this module can be used independently or as part of a larger application. Before we start using it, install it using the command “pip install pythonping.”
pip install pythonping
Below is our first program using pythonping module. It is a simple program that sends ICMP probes to the target machine.
from pythonping import ping
ping('1.1.1.1')
Output
Process finished with exit code 0
The program ran without any errors, but we could not see the output. That is because the program runs in silent mode. If we want to see the output of the ICMP probes sent and received, use verbose and set it to True.
from pythonping import ping
ping('1.1.1.1', verbose=True)
Output
Reply from 1.1.1.1, 29 bytes in 67.98ms
Reply from 1.1.1.1, 29 bytes in 104.35ms
Reply from 1.1.1.1, 29 bytes in 110.87ms
Reply from 1.1.1.1, 29 bytes in 108.5ms
Process finished with exit code 0
Use case : In this program, we created a list of three IP addresses. Using the ping function to ping each IP address from a list and display the result.
from pythonping import ping
IPs = ['1.1.1.1','2.2.2.2','8.8.8.8']
for ip in IPs:
print('Ping to %s'%ip)
ping(ip, verbose=True)
Output
Ping to 1.1.1.1
Reply from 1.1.1.1, 29 bytes in 98.57ms
Reply from 1.1.1.1, 29 bytes in 75.85ms
Reply from 1.1.1.1, 29 bytes in 74.0ms
Reply from 1.1.1.1, 29 bytes in 59.53ms
Ping to 2.2.2.2
Request timed out
Request timed out
Request timed out
Request timed out
Ping to 8.8.8.8
Reply from 8.8.8.8, 29 bytes in 49.0ms
Reply from 8.8.8.8, 29 bytes in 9.3ms
Reply from 8.8.8.8, 29 bytes in 14.68ms
Reply from 8.8.8.8, 29 bytes in 67.98ms
Process finished with exit code 0
Use case : By default, five ICMP probes are sent to the target machine. However, the number of packets sent to the target can be modified using the count value. In this program, we have set the count value to 2.
from pythonping import ping
IP = '1.1.1.1'
ping(IP, count=2,verbose=True)
Output
Reply from 1.1.1.1, 29 bytes in 55.1ms
Reply from 1.1.1.1, 29 bytes in 50.19ms
Process finished with exit code 0
Use case : Ping to multiple IP addresses by setting the count value to 2
from pythonping import ping
IP_Addresses = ['1.1.1.1','3.3.3.3','8.8.8.8']
for ip in IP_Addresses:
print('Ping to %s'%ip)
ping(ip, count=2, verbose=True)
print()
Output
Ping to 1.1.1.1
Reply from 1.1.1.1, 29 bytes in 99.4ms
Reply from 1.1.1.1, 29 bytes in 50.79ms
Ping to 3.3.3.3
Request timed out
Request timed out
Ping to 8.8.8.8
Reply from 8.8.8.8, 29 bytes in 35.19ms
Reply from 8.8.8.8, 29 bytes in 14.6ms
Process finished with exit code 0
Use case :In this program, we will display the maximum latency that appeared while pinging to the target.
from pythonping import ping
response = ping('1.1.1.1')
print('Content of Response variable')
print(response)
print('Maximum Latency',response.rtt_max_ms)
Output
Content of Response variable
Reply from 1.1.1.1, 29 bytes in 59.59ms
Reply from 1.1.1.1, 29 bytes in 60.01ms
Reply from 1.1.1.1, 29 bytes in 55.58ms
Reply from 1.1.1.1, 29 bytes in 55.76ms
Round Trip Times min/avg/max is 55.58/57.74/60.01 ms
Maximum Latency 60.01
Process finished with exit code 0
Use case : In this program, we will ping multiple IP addresses and display the maximum latency for each IP address.
from pythonping import ping
IPAddress = ['1.1.1.1','3.3.3.3','8.8.8.8']
for IP in IPAddress:
response = ping(IP,count=3)
print('Maximum Latency for %s : '%IP,response.rtt_max_ms)
Output
Maximum Latency for 1.1.1.1 : 27.88
Maximum Latency for 3.3.3.3 : 2000
Maximum Latency for 8.8.8.8 : 7.91
Process finished with exit code 0
Use case : Checking minimum and maximum latency for multiple IP addresses
from pythonping import ping
IPAddress = ['1.1.1.1','3.3.3.3','8.8.8.8']
for ip in IPAddress:
response = ping(ip, count=3)
print('Min and Max latency for %s : '%ip,response.rtt_min_ms,'|',response.rtt_max_ms)
Output
Min and Max latency for 1.1.1.1 : 5.91 | 6.7
Min and Max latency for 3.3.3.3 : 2000 | 2000
Min and Max latency for 8.8.8.8 : 21.53 | 58.53
Process finished with exit code 0
Use case : Taking IP address from user and printing the min and max latency for the target
from pythonping import ping
IP = input('Enter a IP address: ')
response = ping(IP, count=3, verbose=True)
print('Min latency:',response.rtt_min_ms)
print('Max latency',response.rtt_max_ms)
Output
Enter a IP address: 8.8.8.8
Reply from 8.8.8.8, 29 bytes in 6.57ms
Reply from 8.8.8.8, 29 bytes in 5.54ms
Reply from 8.8.8.8, 29 bytes in 6.73ms
Min latency: 5.54
Max latency 6.73
gethostbyname() function
There are conditions where we need to find an IP address for a domain name. This can be done quickly by using a socket module’s gethostbyname() function. This function takes the website name or hostname and returns the IP address. If the IP address cannot be found, the socket.gairerror exception is thrown.
import socket
host = ['www.google.com','www.facebook.com','www.yahoo.in']
for h in host:
try:
addr = socket.gethostbyname(h)
print('IP address for %s : %s'%(h,addr))
except socket.gaierror:
print('IP not found for %s'%h)
Output
IP address for www.google.com : 142.250.183.164
IP address for www.facebook.com : 157.240.16.35
IP address for www.yahoo.in : 106.10.248.150
Process finished with exit code 0
Use case : Asking the user for a URL and then using the gethostbyname() function to find the IP address
import socket
host = input('Enter a url to find IP address: ')
try:
addr = socket.gethostbyname(host)
print('IP address for %s : %s'%(host,addr))
except socket.gaierror:
print('IP Address not found')
Output
Enter a url to find IP address: youtube.com
IP address for youtube.com : 172.217.27.206
Process finished with exit code 0