os.system() #输出名结果到屏幕,返回命令执行状态
os.popen().read() #会将命令执行结果输出
subprocess
subprocess.run()
>>> subprocess.run(['df','-h'])
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 12G 6.2G 66% /
tmpfs 499M 0 499M 0% /dev/shm
/dev/sda1 194M 30M 155M 17% /boot
/dev/sdb5 20G 439M 19G 3% /57data
CompletedProcess(args=['df', '-h'], returncode=0)
>>> subprocess.run("df -h | grep sda1",shell=True)
/dev/sda1 194M 30M 155M 17% /boot
CompletedProcess(args='df -h | grep sda1', returncode=0)
##############
执行成功返回0,否则非0
>>> a=subprocess.call("ls -l",shell=True)
total 127920
-rwxr-xr-x 1 root root 796 Sep 27 18:25 aa.sh
-rwxr-xr-x 1 root root 1048 Sep 5 17:45 aa.sh.bak
-rw-r--r-- 1 root root 1160 Sep 6 11:49 aa.txt
-rw-r--r-- 1 root root 5870361 Aug 23 2016 all.sql
-rw-------. 1 root root 3659 Oct 11 2015 anaconda-ks.cfg
-rw-r--r-- 1 root root 25033 Sep 5 17:46 BACKUP_LOG
-rw-r--r-- 1 root root 1584536 Sep 14 12:28 bb
-rw-r--r-- 1 root root 4866122 Sep 14 11:39 custins1405873_1473669277748.csvt
-rw------- 1 root root 3 Sep 6 10:48 dead.letter
-rw-r--r-- 1 root root 10240 Jan 21 2011 ez_setup.py
-rwxr-xr-x 1 zabbix games 8558592 Aug 1 2016 gh-ost
drwxr-xr-x 4 root root 4096 May 31 2016 innotop-gtid
-rw-r--r--. 1 root root 12790 Oct 11 2015 install.log.syslog
drwxr-xr-x 4 root root 4096 Jun 7 2016 mha
-rw-r--r-- 1 root root 14923535 Aug 19 2016 mirror
drwxr-xr-x 7 root root 4096 Jun 27 2016 mycat
-rw-r--r-- 1 root root 1977 Jun 26 2016 my.cnf.example
-rw-r--r-- 1 root root 51433090 Feb 22 12:16 mysql-5.7.17.tar.gz
drwxr-xr-x 17 1000 1000 4096 Feb 24 17:25 Python-3.5.3
-rw-r--r-- 1 root root 20656090 Feb 24 15:02 Python-3.5.3.tgz
-rw-r--r-- 1 root root 570202 Sep 13 10:20 rswb.sql
drwxr-xr-x 2 root root 4096 Jun 21 2016 software
-rw-r--r-- 1 root root 358285 Jun 20 2016 sysbench-0.5.zip
-rw-r--r-- 1 root root 3619 Aug 29 2016 test.sql
-rw-r--r-- 1 root root 60 Sep 2 16:12 tt.sh
-rw-r--r-- 1 root root 22037899 Sep 2 14:59 zabbix-3.0.3.tar.gz
>>> a
0
############
命令执行结果为元组形式,第一个元素为执行状态,0表示执行成功,非零表示失败,第二个元素为执行结果
>>> a=subprocess.getstatusoutput('df -h')
>>> a
(0, 'Filesystem Size Used Avail Use% Mounted on\n/dev/sda3 19G 12G 6.2G 66% /\ntmpfs 499M 0 499M 0% /dev/shm\n/dev/sda1 194M 30M 155M 17% /boot\n/dev/sdb5 20G 439M 19G 3% /57data')
>>> print(a[0])
0
>>> print(a[1])
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 12G 6.2G 66% /
tmpfs 499M 0 499M 0% /dev/shm
/dev/sda1 194M 30M 155M 17% /boot
/dev/sdb5 20G 439M 19G 3% /57data
>>>
#################
按字符串格式接收命令,并返回结果。
>>> subprocess.getoutput('pwd')
'/root'
################
执行命令并返回结果,结果为bites格式
>>> subprocess.check_output('pwd')
b'/root\n'
>>> a=subprocess.check_output('pwd')
>>> print(a)
b'/root\n'
###########
>>> a=subprocess.Popen("ifconfig -afads",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> a.stderr.read()
b"ifconfig: option `-afads' not recognised.\nifconfig: `--help' gives usage information.\n"
>>> a.stdout.read()
b''
>>>
##############
命令未执行完poll()的值为none,执行完未0
>>> a=subprocess.Popen("sleep 20; echo 'hello'",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> a.poll()
>>> a.poll()
0
>>> a.poll()
0
命令未执行完,wait()会一直等待,执行完后,wait()返回值为0
>>> a=subprocess.Popen("sleep 20; echo 'hello'",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> a.wait()
0
terminate()中断命令执行,中断后wait()和poll()返回值为-15
>>> a=subprocess.Popen("sleep 20; echo 'hello'",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> a.terminate()
>>> a.wait()
-15
>>> a.poll()
-15
>>> a.stdout.read()
b''
>>>
echo '1232' | sudo -S server network start