-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
73 lines (65 loc) · 2.19 KB
/
test.py
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
# -*- coding: utf-8 -*-
# Author : Yang Hao
# File : test.py
# Software: PyCharm
# Time : 2020/3/20 18:12
# Description:
# import hashlib
# import subprocess
#
# import hashlib
#
# salt = 'python-flask'
# password = 'admin'
# md5 = hashlib.md5()
# md5.update((password + salt).encode('utf8'))
#
# print(md5.hexdigest())
#
# import re
#
# pattern = re.compile(r'^(?![0-9]+$)(?![^0-9]+$)(?![a-zA-Z]+$)(?![^a-zA-Z]+$)(?![a-zA-Z0-9]+$)[a-zA-Z0-9\S]{8,20}$')
# str = '6yhn^YHN'
# print(pattern.search(str))
import subprocess
print("---------------1---------------")
# subprocess.run 接受字符串或者列表形式的命令,返回命令执行结果和状态码
ret = subprocess.run(["df", "-h"])
print(ret)
print('\n')
print("---------------2---------------")
# subprocess.call() 执行命令,返回命令执行结果和状态码
# ret = subprocess.call(["ls", "-l"], shell=False) # shell为False的时候命令必须分开写
# ret = subprocess.call("ls -l", shell=True)
ret = subprocess.call(["ls", "-l"])
print(ret)
print('\n')
print("---------------3---------------")
# subprocess.check_call() 执行命令,返回结果和状态,正常为0;执行错误则抛出异常
ret = subprocess.check_call(["ls", "-l"])
print(ret)
print('\n')
print("---------------4---------------")
# subprocess.getstatusoutput() 接受字符串形式的命令,返回一个元组形式的结果:第一个元素是状态码,第二个为执行结果
ret = subprocess.getstatusoutput('pwd')
print(ret)
print('\n')
print("---------------5---------------")
# subprocess.getoutput() 接受字符串形式的命令,返回执行结果
ret = subprocess.getoutput('pwd')
print(ret)
print('\n')
print("---------------6---------------")
# subprocess.check_output() 执行命令,返回运行结果,而不是打印
ret = subprocess.check_output('pwd')
print(ret)
print('\n')
print("---------------7---------------")
# subprocess.Popen 用于执行复杂的系统命令
# obj = subprocess.Popen("mkdir t3", shell=True, cwd='/opt')
# print(obj)
password = 'xxxxx'
command = 'mkdir t4'
ret = subprocess.call('cd /opt', shell=True)
subprocess.call('echo {} | sudo -S {}'.format(password, command), shell=True, cwd='/opt')
ret = subprocess.call('pwd')