As of this writing, Linux has a global desktop market share of 2.77% (A Report by Statcounter), but it powers over 90% of all cloud infrastructure and hosting services. It is critical to be familiar with common Linux commands for this reason alone.
According to a 2022 StackOverflow survey, Linux-based operating systems are more popular than macOS, demonstrating the appeal of using open-source software by professional developers, with an impressive 39.89% market share.
Linux is a popular operating system among software developers and system administrators. It is widely used because it is open source, free and customizable, extremely robust, and adaptable. This makes it an excellent choice for servers, virtual machines (VMs), and a variety of other applications.
As a result, knowing how to work with Linux is essential for anyone working in the tech industry because it is used almost everywhere.
On the other hand, Python is a well-known and up-to-date programming language. It is especially popular among beginners because it is simple to learn. We can use Python to interact with the operating system because it is so powerful. Python serves as an alternative to the bash command language for scripting in Linux. Because it is a dependency on many tools and software, it is preinstalled in the majority of distributions. If not, it is simple to install. In this blog, we will demonstrate how to execute Linux commands in Python. Let’s get started! 🚀
What Is a Linux Command?
A command in Linux is a programme or utility that runs from the command line. A command line is an interface that accepts lines of text and converts them into computer instructions.
Any graphical user interface (GUI) is simply a graphical representation of command-line programmes. For example, when you close a window by clicking the “X,” a command is executed behind the scenes.
Recent Stats on Linux
Few Most Commonly Used Linux Commands
- ls Command
- alias Command
- unalias Command
- pwd Command
- cd Command
- cp Command
- rm Command
- mv Command
- mkdir Command
- man Command
- touch Command
- chmod Command
- ./ Command
- exit Command
- sudo Command
- shutdown Command
- htop Command
- unzip Command
- apt , yum , pacman Commands
- echo Command
- cat Command
- ps Command
- kill Command
- ping Command
- vim Command
Hands-on
In this hands-on, we will see how we can execute the ping Linux command in Python. We will begin with the creation of an empty directory. Creating a text file, we will store it in several valid server domains within the file. Then we will install a package if it doesn’t exist. Moving further, we will add the code creating a new file for the code. We will then alter the code based on whether or not we have administrator rights. Post that we will run the server and test out the execution for a single valid server followed by testing out the same for an invalid server. Altering the code, we will then execute the codebase onto a list of valid servers. On success, we will change the server list file to fill in valid and invalid servers. Post testing the same, finally we will pass in only invalid servers to test out the code.
Note: Please ensure that Python is properly installed onto the local machine or else it might display multiple errors while following the process.
Create a new directory on the local machine.
Open the directory in the code editor.
Right-click in the folder structure and click on the new file.
Enter a name for the file as shown below. We need to create a text file.
In the newly created text file, add some domains within it as shown below.
Right-click in the folder structure and again click on New File.
Create a .py file where we will be having our code ping servers via python using the Linux command.
The subprocess package is usually inbuilt but in case you face any issues, run the below command to install it in the directory:
pip install subprocess.run
Now, in the newly created Python file, import the subprocess package.
Define a new function as shown in the image below. This function will contain the code to ping the servers and return the response.
Add a new variable with the command that we wish to execute.
If you have administrator access, add the following code so that the ping command sends only one package.
We will then convert the output into the string format.
Finally, we’ll append the result to the list of results and return the list.
Initializing the main function, we will call the function that we created above passing a domain as the parameter.
The final code looks like as shown in the image below.
On success, we will run the command as shown below to execute the code.
Note that if you do not have administrator rights, you will get an error as shown in the image below.
To avoid that error, you can remove the highlighted part from the code base.
The only difference is that using the below command will send multiple packets while executing the ping command.
Run the codebase using the below command.
On success, if it is a valid domain, you will see the following in the console.
Now, let’s change the code passing an invalid domain.
Run the code using the below command.
Since it was an invalid domain, it displays the below result.
Now, since we created a txt file with some domains in it, we will open that file and read the domains in it.
Execute the codebase.
On success, you will see the list of servers in the txt file in an array format.
Use the below code to perform a strip operation through all the servers.
Run the code using the below command.
You will get to see the servers in an array format.
Now, finally, call the execute command function that we created above passing the servers list as the parameter, and print the final output.
Make the changes as follows in the function code adding a loop to iterate over all servers in the file.
Run the codebase using the below command.
You will see the successful execution of the ping command for all the servers.
Enter a combination of valid and invalid servers in the server.txt file.
Run the server again.
On success, you will see the valid output of the ping command results.
For the final testing, enter 2 invalid servers.
Run the codebase again.
You will see the successful result for the following execution.
Conclusion
In this hands-on, we saw how we can execute the ping Linux command in Python. We began with the creation of an empty directory. Creating a text file, we stored several valid server domains within the file. Then we installed a package if it didn’t exist. Moving further, we added the code creating a new file for the code. We then altered the code based on whether or not we had administrator rights. Post that we ran the server and tested out the execution for a single valid server followed by testing out the same for an invalid server. Altering the code, we then executed the codebase onto a list of valid servers. On success, we changed the server list file to fill in valid and invalid servers. Post testing the same, finally we passed in only invalid servers to test out the code. We will come up with more such use cases in our upcoming blogs.
Meanwhile…
If you are an aspiring Python Developer and want to explore more about the above topics, here are a few of our blogs for your reference:
- How to connect Snowflake with Python and execute queries?
- How to easily build ETL Pipeline using Python and Airflow?
- How to send Webhooks using Python and receive via Node.js Applications?
- How to build a Web Scraper using Python?
Stay tuned to get all the updates about our upcoming blogs on the cloud and the latest technologies.
Keep Exploring -> Keep Learning -> Keep Mastering
At Workfall, we strive to provide the best tech and pay opportunities to kickass coders around the world. If you’re looking to work with global clients, build cutting-edge products and make big bucks doing so, give it a shot at workfall.com/partner today!