Example of python executing terminal and console commands
- 2021-07-16 02:42:40
- OfStack
As shown below:
import os
os.system()
os.popen().read().strip()
# Above 2 Methods Yes python Execution terminal / Console Common methods of commands
#os.system('ping www.baidu.com') Successful execution Return 0
#ping = os.popen('pint www.baidu.com').read().strip() Return the output result
# Note: os.system() Execution completion Will close So when performing the follow-up Command needs to depend on the previous command, write multiple commands to the 1 A os.system() Inside
# But When this method is executed, Is unable to interact with each other For example Insufficient command permissions You need to enter a login password You can use the following method
import pexpect
ch = pepect.spwn(' Command ')
ch.expect('Password:')
ch.sendline(' Password ')