-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsubprocess_in_python.py
More file actions
53 lines (45 loc) · 1.26 KB
/
Copy pathsubprocess_in_python.py
File metadata and controls
53 lines (45 loc) · 1.26 KB
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
#running a simple shell command
import subprocess
#list all files in a directory
"""
#for linux
result = subprocess.run(['ls', '-l'], shell=True,
capture_output=True, text=True)
print(result.stdout)
#for windows
result = subprocess.run(['dir'], shell=True,
capture_output=True, text=True)
print(result.stdout)
"""
#checking disk space
"""
#for linux
result = subprocess.run(['df', '-h'], shell=True,
capture_output=True, text=True)
print(result.stdout)
#for windows
result = subprocess.run(['tasklist'], shell=True,
capture_output=True, text=True)
print(result.stdout)
"""
#running an external program
#linux
server = "google.com"
result = subprocess.run(["ping", "-c", "4", server],
capture_output=True, text=True)
if result.returncode == 0:
print("Server is reachable")
print(result.stdout)
else:
print("Server is unreachable")
print(result.stderr)
#windows
server = "google.com"
result = subprocess.run(["ping", "-n", "4", server],
capture_output=True, text=True)
if result.returncode == 0:
print("Server is reachable")
print(result.stdout)
else:
print("Server is unreachable")
print(result.stderr)