python模块-执行系统命令

摘要

可以执行shell命令的相关模块和函数有:

  • os.system
  • os.spawn*
  • os.popen* –废弃
  • popen2.* –废弃
  • commands.* –废弃,3.x中被移除
  • subprocess

commands

1
2
3
4
5
6
import commands

result = commands.getoutput('cmd')
result = commands.getstatus('cmd')
result = commands.getstatusoutput('cmd')
(status, out) = commands.getstatusoutput('cmd' + ' 2>&1')

以上执行shell命令的相关的模块和函数的功能均在 subprocess 模块中实现,并提供了更丰富的功能。

subprocess

call

执行命令,返回状态码

1
2
ret = subprocess.call(["ls", "-l"], shell=False)
ret = subprocess.call("ls -l", shell=True)

shell = True ,允许 shell 命令是字符串形式

check_call

执行命令,如果执行状态码是 0 ,则返回0,否则抛异常

1
2
subprocess.check_call(["ls", "-l"])
subprocess.check_call("exit 1", shell=True)

check_output

执行命令,如果状态码是 0 ,则返回执行结果,否则抛异常

1
2
subprocess.check_output(["echo", "Hello World!"])
subprocess.check_output("exit 1", shell=True)

subprocess.Popen(…)

用于执行复杂的系统命令

参数:

  • args:shell命令,可以是字符串或者序列类型(如:list,元组)
  • bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
  • stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄
  • preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
  • close_sfs:在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
  • shell:同上
  • cwd:用于设置子进程的当前目录
  • env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
  • universal_newlines:不同系统的换行符不同,True -> 同意使用 \n
  • startupinfo与createionflags只在windows下有效
    将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等
1
2
3
import subprocess
ret1 = subprocess.Popen(["mkdir","t1"])
ret2 = subprocess.Popen("mkdir t2", shell=True)

终端输入的命令分为两种:

  • 输入即可得到输出,如:ifconfig
  • 输入进行某环境,依赖再输入,如:python
1
2
3
import subprocess

obj = subprocess.Popen("mkdir t3", shell=True, cwd='/home/dev',)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import subprocess

obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
obj.stdin.write('print 1 \n ')
obj.stdin.write('print 2 \n ')
obj.stdin.write('print 3 \n ')
obj.stdin.write('print 4 \n ')
obj.stdin.close()

cmd_out = obj.stdout.read()
obj.stdout.close()
cmd_error = obj.stderr.read()
obj.stderr.close()

print cmd_out
print cmd_error
1
2
3
4
5
6
7
8
9
10
import subprocess

obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
obj.stdin.write('print 1 \n ')
obj.stdin.write('print 2 \n ')
obj.stdin.write('print 3 \n ')
obj.stdin.write('print 4 \n ')

out_error_list = obj.communicate()
print out_error_list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import subprocess

obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out_error_list = obj.communicate('print "hello"')
print out_error_list
import subprocess

'''
sh-3.2# ls /Users/egon/Desktop |grep txt$
mysql.txt
tt.txt
事物.txt
'''

res1=subprocess.Popen('ls /Users/jieli/Desktop',shell=True,stdout=subprocess.PIPE)
res=subprocess.Popen('grep txt$',shell=True,stdin=res1.stdout,
stdout=subprocess.PIPE)

print(res.stdout.read().decode('utf-8'))


#等同于上面,但是上面的优势在于,一个数据流可以和另外一个数据流交互,可以通过爬虫得到结果然后交给grep
res1=subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',shell=True,stdout=subprocess.PIPE)
print(res1.stdout.read().decode('utf-8'))


#windows下:
# dir | findstr 'test*'
# dir | findstr 'txt$'
import subprocess
res1=subprocess.Popen(r'dir C:\Users\Administrator\PycharmProjects\test\函数备课',shell=True,stdout=subprocess.PIPE)
res=subprocess.Popen('findstr test*',shell=True,stdin=res1.stdout,
stdout=subprocess.PIPE)

print(res.stdout.read().decode('gbk')) #subprocess使用当前系统默认编码,得到结果为bytes类型,在windows下需要用gbk解码

系统命令例子

安装cmdb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import os
import subprocess
import argparse
import time

def base(cmd):
if subprocess.call(cmd, shell=True):
raise Exception("{} 执行失败".format(cmd))

def install_docker():
base("sudo yum install -y yum-utils device-mapper-persistent-data lvm2")
base("sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo")
base("sudo yum makecache fast")
base("sudo yum -y install docker-ce")
if(not os.path.exists("/etc/docker")):
base("mkdir -p /etc/docker")
with open("/etc/docker/daemon.json", "w") as f:
f.write('{\n "registry-mirrors": ["https://9f4w4icn.mirror.aliyuncs.com"] \n}')
base("sudo systemctl daemon-reload")
base("sudo systemctl start docker")

def create_dir():
if (not os.path.exists("/var/cmdb/db")):
base("sudo mkdir -p /var/cmdb/db")
if (not os.path.exists("/var/cmdb/es")):
base("sudo mkdir -p /var/cmdb/es")

def run_db_container():
base("sudo docker run --name cmdb-db -d -e MYSQL_ROOT_PASSWORD=cmdbcmdb -v /var/cmdb/db:/var/lib/mysql mysql:5.7.21")

def run_es_container():
base("sudo docker run --name cmdb-es -d -v /var/cmdb/es:/usr/share/elasticsearch/data elasticsearch:5.6.8")

def init_db():
base("sudo docker run -it --rm --link cmdb-db -e DB_HOST=cmdb-db -e ENV=PRO -e DB_PORT=3306 -e DB_USERNAME=root -e DB_PASSWORD=cmdbcmdb -e DB_NAME=cmdb mingmingtang/cmdb init-db")

def run_cmdb_container(site_url, email_host, email_port, email_username, email_password):
base("sudo docker run -d --name cmdb --link cmdb-db --link cmdb-es -p 80:80 -e ENV=PRO -e SITE_URL={} -e DB_HOST=cmdb-db -e DB_PORT=3306 -e DB_USERNAME=root -e DB_PASSWORD=cmdbcmdb -e DB_NAME=cmdb -e ELASTICSEARCH_HOSTS=cmdb-es -e EMAIL_HOST={} -e EMAIL_PORT={} -e EMAIL_USERNAME={} -e EMAIL_PASSWORD={} mingmingtang/cmdb start".format(site_url, email_host, email_port, email_username, email_password))

def input_para(help):
value = ""
while(not value):
value = raw_input(help)
return value

if __name__ == '__main__':
if(os.geteuid() != 0):
raise("请以root权限运行")
# parser = argparse.ArgumentParser()
# parser.add_argument("--siteurl", type=str, help="E.g: http://cmdb.xxx.com, http://172.17.100.1")
# parser.add_argument("--emailhost", type=str, help="E.g: http://cmdb.xxx.com, http://172.17.100.1")
# parser.add_argument("--emailport", type=str, help="E.g: http://cmdb.xxx.com, http://172.17.100.1")
# parser.add_argument("--emailusername", type=str, help="E.g: http://cmdb.xxx.com, http://172.17.100.1")
# parser.add_argument("--emailpassword", type=str, help="E.g: http://cmdb.xxx.com, http://172.17.100.1")
# args = parser.parse_args()
# SITE_URL = args.SITE_URL

site_url = input_para("请输入网站域名或IP(http://cmdb.xxx.com):")
email_host = input_para("网站邮箱服务器(smtp.163.com):")
email_port = input_para("邮箱服务器端口(25):")
email_username = input_para("邮箱用户名(cmdb@163.com):")
email_password = input_para("邮箱密码|独立授权码(P@ssw0rd):")

print("开始安装docker")
install_docker()
print("开始创建目录")
create_dir()
print("开始运行mysql容器")
run_db_container()
print("开始运行elasticsearch容器")
run_es_container()
print("等待数据库启动完成(10s)")
time.sleep(10)
print("开始初始化数据库")
init_db()
print("开始运行cmdb")
run_cmdb_container(site_url, email_host, email_port, email_username, email_password)
print("完成!")
Donate