Ping everywhere.
In my other Linux series, Linux programming, I’ve written about how we can access the kernel’s API to interact with files stored within Linux. Now as we learn through network automation skills, we’ll re-learn what we did in C with the new language, Python. Let’s dive into it!
As someone who’s interacting with multiple Linux servers on a daily bases, it’s always better to have at least a minimum of network knowledge and skills. And here’s one of the very basic networking automation skills that take advantage of Linux and Python.
Very basics:
As you can guess from the codes, what’s done here is that the program first scans through the servers’ list and send ping signals to each IP address listed in the list.
network_auto.py
import subprocess
servers = ["192.168.0.104", "192.168.0.107", "192.168.0.105"]
for hostname in servers:
response = subprocess.run(["ping", "-c", "1", hostname], capture_output=True, text=True)
if "1 packets transmitted, 1 received" in response.stdout:
print(f"{hostname} is online.")
else:
print(f"{hostname} is offline.")
The executed result:
$python3 network_auto.py
server1.com is offline.
server2.com is online.
server3.com is offline.
Reading another file’s list
This is a little advanced from the previous one. In line 3, it opens a file in ‘read’ mode and scans through the servers’ list, and sends ping signals to each IP address listed in the list.
network_auto2.py
import subprocess
with open("hostname_list.txt", "r") as f:
servers = f.read().splitlines()
for hostname in servers:
response = subprocess.run(["ping", "-c", "1", hostname], capture_output=True, text=True)
if "1 packets transmitted, 1 received" in response.stdout:
print(f"{hostname} is online.")
else:
print(f"{hostname} is offline.")
hostname_list.txt
192.168.0.104
192.168.0.107
192.168.0.105
The executed result:
$python3 network_auto2.py
192.168.0.104 is online.
192.168.0.107 is online.
192.168.0.105 is online.
Reading another file’s list as an argument
This is a little advanced version of the previous one. This one offers more flexibility when it comes to selecting the external list file since it can be specified as the argument when executing it.
if len(sys.argv) < 2:
print("Please specify a file containing the list of servers.")
sys.exit(1)
filename = sys.argv[1]
try:
with open(filename, "r") as f:
servers = f.read().splitlines()
except FileNotFoundError:
print(f"Error: The file {filename} could not be found.")
sys.exit(1)
for hostname in servers:
response = subprocess.run(["ping", "-c", "1", hostname], capture_output=True, text=True)
if "1 packets transmitted, 1 received" in response.stdout:
print(f"{hostname} is online.")
else:
print(f"{hostname} is offline.")
hostname_list.txt
192.168.0.104
192.168.0.107
192.168.0.105
The executed result:
$python3 network_auto3.py hostname_list.txt
192.168.0.104 is online.
192.168.0.107 is online.
192.168.0.105 is online.
Reading a file’s list as an argument and logging the result in another result file:
import subprocess
import sys
if len(sys.argv) < 3:
print("Please specify a file containing the list of servers and a file to write the results.")
sys.exit(1)
servers_file = sys.argv[1]
results_file = sys.argv[2]
try:
with open(servers_file, "r") as f:
servers = f.read().splitlines()
except FileNotFoundError:
print(f"Error: The file {servers_file} could not be found.")
sys.exit(1)
results = []
for hostname in servers:
response = subprocess.run(["ping", "-c", "1", hostname], capture_output=True, text=True)
if "1 packets transmitted, 1 received" in response.stdout:
results.append(f"{hostname} is online.")
else:
results.append(f"{hostname} is offline.")
try:
with open(results_file, "w") as f:
for result in results:
f.write(result + "\n")
print(f"Results written to {results_file}.")
except FileNotFoundError:
print(f"Error: The file {results_file} could not be found.")
sys.exit(1)
hostname_list.txt
192.168.0.104
192.168.0.107
192.168.0.105
The executed result:
$python3 network_auto4.py hostname_list.txt result_list.txt
Results written to result_list.txt.
result_list.txt
192.168.0.104 is online.
192.168.0.107 is online.
192.168.0.105 is online.
Afterthoughts:
What we’ve seen here is not only about network automation, but also about file controls, such as open, read and write, which is something I’ve written in C language in another post. Hopefully, you can take advantage of those skills in many other sectors of Linux as well.