How to execute an external shell script or command in python.
This time, we assume the operation of executing "ip-address_check.sh" to check whether the IP address entered in the python code is correct, and storing and displaying the result.
When it comes to executing an external shell script, there are two patterns, but the return values are different.
os.system('ls -la') If the command is successful, 0 will be returned.
commands.getoutput("ls -la") The result of the actual execution will be returned.
#!/usr/bin/env python
import commands
check = commands.getoutput("./ip-address_check.sh 192.168.1.1")
print check
Since ip-address_check is created to return OK if there is no problem with the format, in this case print check will also output OK as the output result of python.
It's easy to forget the command, so make a note of it.
Recommended Posts