Yesterday, we discussed about the Python module “subprocess” and its classes to manage Linux commands. Before doing the exercises below, I would like to tell you to refer the previous article on subprocess. You will get basic ideas from there. Please see this article for more details > The module subprocess in Python
This is a Python module for administration tasks. This module should be used for accessing system commands. This is the simplest way of running Linux commands. The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. Bookmark the Index page and read more about Python.
In this post, I am going through some exercise on Python subprocess module and its commonly using classes.
Read the usages of “subprocess.Popen,” “subprocess.PIPE” and “.communicate” from subprocess classes.
Example 1
Print “Hello World!” using subprocess.Popen
The class subprocess.Popen is replacing os.popen. This class uses for process creation and management in the subprocess module.
Linux command:
echo "Hello World"
In [1]: import subprocess
In [2]: hello = subprocess.Popen(["echo", "Hello World"], stdout=subprocess.PIPE)
In [3]: hw = hello.communicate()
In [4]: print hw
('Hello World\n', None)
By creating .py file on Shell
Step 1 : SSH to server as root.
Step 2 : Create a file with .py extension.
Step 3 : Add the following code:
#!/usr/bin/python
## Printing hello world using subprocess.Popen
import subprocess
h = subprocess.Popen(["echo", "Hello World"], stdout=subprocess.PIPE)
hello = h.communicate()
print hello
Example 2
Pinging a host using Python script
Linux command:
ping -c 2 IP.Address
In [1]: import subprocess
In [2]: host = raw_input("Enter a host IP address to ping: ")
Enter a host IP address to ping: 8.8.4.4
In [3]: p = subprocess.Popen(['ping', '-c 2', host], stdout=subprocess.PIPE)
In [4]: ping = p.communicate()
In [5]: print ping
('PING 8.8.4.4 (8.8.4.4) 56(84) bytes of data.\n64 bytes from 8.8.4.4: icmp_seq=1 ttl=48 time=16.0 ms\n64 bytes from 8.8.4.4: icmp_seq=2 ttl=48 time=16.1 ms\n\n--- 8.8.4.4 ping statistics ---\n2 packets transmitted, 2 received, 0% packet loss, time 1001ms\nrtt min/avg/max/mdev = 16.016/16.102/16.188/0.086 ms\n', None)
By creating py file on Shell
Step 1 : SSH to server as root.
Step 2 : Create a file with .py extension.
Step 3 : Add the following code:
#!/usr/bin/python
import subprocess
##Inserting host IP using raw_input
host = raw_input("Enter a host IP address to ping: ")
##Create and execute subprocess
h = subprocess.Popen(['ping', '-c 4', host], stdout=subprocess.PIPE)
ping = h.communicate()
print ping
Example 3
Check IP address status on a server has CSF firewall
Linux command:
csf -g IP.Address
In Python
In [1]: import subprocess
In [2]: host = raw_input("Enter a host IP address to check firewall status: ")
Enter a host IP address to check firewall status: 8.8.8.8
In [3]: cs = subprocess.Popen(['csf', '-g', host], stdout=subprocess.PIPE)
In [4]: csf = cs.communicate()
In [5]: print csf
('*WARNING* URLGET set to use LWP but perl module is not installed, reverting to HTTP::Tiny\n\nChain num pkts bytes target prot opt in out source destination \nNo matches found for 8.8.8.8 in iptables\n\n\nip6tables:\n\nChain num pkts bytes target prot opt in out source destination \nNo matches found for 8.8.8.8 in ip6tables\n*WARNING* TESTING mode is enabled - do not forget to disable it in the configuration\n', None)
By creating py file on Shell
Step 1 : SSH to server as root.
Step 2 : Create a file with .py extension.
Step 3 : Add the following code:
#!/usr/bin/python
import subprocess
##Inserting host IP using raw_input
host = raw_input("Enter a host IP address to check firewall status: ")
cs = subprocess.Popen(['csf', '-g', host], stdout=subprocess.PIPE)
csf = cs.communicate()
print csf
That’s it!
Please try it and share your worries with us!