top of page

How To Build A Python Ping Script On A Mac Computer


INTRODUCTION

Regardless of whether you are in application development or network security, every cybersecurity professional should know how to perform the basic function of a ping command or know how to build a python ping script. The python ping script is an important tool that infosec professionals can use to get a more detailed reading of a network. They can use the ping script to see if a certain device is connected or whether a server is active. In this post, the tutorial and applications of a python ping script are explored.


WHAT IS A PYTHON PING SCRIPT

To start, it is essential to know the definition and applications of the command term “ping”. Ping is a command-line utility that is available on all operating systems, and is used to test the connectivity of devices to a network. When the ping command is typed into a terminal followed by an IP address, it sends a request to the device belonging to that address.

If the pinged device sends a response back to the original computer, then it is official that the pinged device is connected to the network. For a verification program, the concept is the same: A host computer sends a ping request to a remote server. If the server sends back a response to the host computer, then it is official that the server is active. An example ping is shown below:


This form of communication between hosts on a network is referred to as an “echo request” and an “echo reply”. From the “echo reply”, we can determine how far away the remote host is based on the number of hops that the ping had to take in order to connect to the target. Also shown in a reply is the number of packets lost, the number of packets received, and the time taken for the remote host to respond.

Now that the “ping” command has been defined, we can better explain the definition of a python ping script. A ping script is a short program written in the coding language of python that is used to ping IP addresses on the network.


WHY A PYTHON PING SCRIPT IS USEFUL FOR CYBER PROFESSIONALS

A python ping script is a useful program to know how to build mainly for the principles of time and effort. If we needed to ping 10 IP addresses to get a quick reading of which devices are on a network, it would be unproductive to have to type the command “ping” into the terminal for each IP address. Instead, it is more efficient to build a python program that will apply the ping command for however many times we need it.


Building a personal ping program is also useful for the sake of organization. By building our own python program, we can adjust the spacing, add labels, and give a clearer structure to the outputs produced. This makes our results easier to look at and understand.


INSTRUCTIONS: HOW TO CODE

Coding may be daunting for those who do not have much experience, but it is relatively easy for anyone to learn. It is highly suggested before going through this tutorial that you review a few of the basics of python and maybe try a few practice exercises to get the hang of it. There are numerous ways to build a python ping script, with many different methods shared on github or geeksforgeeks. However, the following tutorial was selected from stackoverflow mainly for its simplicity and brevity (two great qualities that a programmer can appreciate). Here are the following directions to create a python ping script on a mac:

  1. Open a new textedit file

  2. Name it ‘ping.py’

  3. Go to the format tab in textedit and click on ‘Make Plain Text’. This allows a file to be run as a python script in the terminal (rtf does not work for running python programs).

  4. Copy and paste the following code below into the textedit file.

import os

hostnames = ["10.40.161.2","10.40.161.3","10.40.161.4"]

for hostname in hostnames:

response = os.system('ping -c 1 ' + hostname)

if response == 0:

print hostname, "PING SUCCESS. HOST IS ACTIVE"

else:

print hostname, "PING FAILED. HOST IS DOWN"

  1. Insert the IP addresses that you wish to ping in the ‘hostnames’ list.

  2. A FOR loop is used, meaning that for each IP address that is entered into the hostnames list, a ping request will be sent.

  3. If the response == 0, an output message is returned confirming that the IP address or server is active.

  4. If the response is anything else other than 0, then an output message is returned confirming that the IP address or server is inactive. Thus, this means that there is no connectivity.

HOW TO RUN THE PYTHON SCRIPT
  1. Save and exit out of the textedit script.

  2. Open terminal by searching for it in your search bar or by going to your launchpad and opening the application from there.

  3. Type the command ‘cd’ (change directory) Desktop.

  4. While in the ‘Desktop’ directory, ‘cd’ to the directory folder that you have saved your textedit file in.

  5. Type ‘python ping.py’ into the terminal command line. The python program should then start to execute.

  6. When the pings are finished executing, you should see the following output in your terminal window:



Congratulations! You have accomplished your very first python ping script!


CONCLUSION

Overall, knowing how to code a little python is an extreme advantage. By practicing a coding language, you can build a program to solve any problem possible! In this case, infosec professionals can use python to build a python ping script; helping them achieve easier, faster, and higher quality results whenever a network or server needs to be evaluated. Hopefully, you enjoyed this tutorial of how to build a python ping script! Goodluck trying it out!


SOURCES

bottom of page