The module subprocess in Python

Method II : Direct method

Inserting commands directly instead as string. This is something similar to the first method. You can simply put the commands directly into the subprocess.call function. Bookmark the Index page and read more about Python.

See the examples pasted below:

To get Kernel details: Eg 1

subprocess.call(["uname", "-a"])

To display disk usage details: Eg 2

subprocess.call(["df", "-h"])

Method III : Executing as Python script in a file with .py extension

Step 1 : SSH to server.
Step 2 : Create a file with .py extension.
Step 3 : Give x permission.

chmod +x file.py

Step 4 : Create code like:

#!/usr/bin/python
import subprocess
print "Total disk details using df command:"
subprocess.call(["df", "-h"])

Step 5 : Execute it:

python filename.py

You got it!!

Try it and let me know if you have any questions..

Post navigation

Arunlal A

Engineer. Linux enthusiast. Traveller. DevOps learner and practitioner. I enjoy working with Linux, DevOps, automation, and technology while exploring new places along the way. I use this space to share my experiences, learnings, experiments, and things I discover along the journey. Whether you're a seasoned DevOps professional or just getting started, let's connect, learn, and grow together. Happy coding, automating, and deploying!

Leave a Reply

Your email address will not be published. Required fields are marked *