martes, 30 de enero de 2018

Python Execute Unix / Linux Command Examples

Python Execute Unix / Linux Command Examples

https://www.cyberciti.biz/faq/python-execute-unix-linux-command-examples/

os.system example (deprecated)

The syntax is:
import os
os.system("command")
In this example, execute the date command:
import os
os.system("date")
Sample outputs:
Sat Nov 10 00:49:23 IST 2012
0
In this example, execute the date command using os.popen() and store its output to the variable called now:
import os
f = os.popen('date')
now = f.read()
print "Today is ", now
Sample outputs:
Today is  Sat Nov 10 00:49:23 IST 2012

Say hello to subprocess

The os.system has many problems and subprocess is a much better way to executing unix command. The syntax is:
import subprocess
subprocess.call("command1")
subprocess.call(["command1", "arg1", "arg2"])
In this example, execute the date command:
import subprocess
subprocess.call("date")
Sample outputs:
Sat Nov 10 00:59:42 IST 2012
0
You can pass the argument using the following syntax i.e run ls -l /etc/resolv.confcommand:
import subprocess
subprocess.call(["ls", "-l", "/etc/resolv.conf"])
Sample outputs:
<-rw-r--r-- 1 root root 157 Nov  7 15:06 /etc/resolv.conf
0
To store output to the output variable, run:
import subprocess
p = subprocess.Popen("date", stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
print "Today is", output
Sample outputs:
Today is Sat Nov 10 01:27:52 IST 2012
Another example (passing command line args):
import subprocess
p = subprocess.Popen(["ls", "-l", "/etc/resolv.conf"], stdout=subprocess.PIPE)
output, err = p.communicate()
print "*** Running ls -l command ***\n", output
Sample outputs:
*** Running ls -l command ***
-rw-r--r-- 1 root root 157 Nov  7 15:06 /etc/resolv.conf
In this example, run ping command and display back its output:
import subprocess
p = subprocess.Popen(["ping", "-c", "10", "www.cyberciti.biz"], stdout=subprocess.PIPE)
output, err = p.communicate()
print  output
The only problem with above code is that output, err = p.communicate() will block next statement till ping is completed i.e. you will not get real time output from the ping command. So you can use the following code to get real time output:
import subprocess
cmdping = "ping -c4 www.cyberciti.biz"
p = subprocess.Popen(cmdping, shell=True, stderr=subprocess.PIPE)
while True:
    out = p.stderr.read(1)
    if out == '' and p.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()
Sample outputs:
PING www.cyberciti.biz (75.126.153.206) 56(84) bytes of data.
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=1 ttl=55 time=307 ms
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=2 ttl=55 time=307 ms
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=3 ttl=55 time=308 ms
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=4 ttl=55 time=307 ms
 
--- www.cyberciti.biz ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 307.280/307.613/308.264/0.783 ms

Related media



No hay comentarios:

Publicar un comentario