forked from AbeTavarez/Python_DevOps_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_date.py
More file actions
30 lines (20 loc) Β· 693 Bytes
/
Copy pathget_date.py
File metadata and controls
30 lines (20 loc) Β· 693 Bytes
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
#!/usr/bin/env python3
import subprocess
# * Running basic methods
subprocess.run(['date'])
subprocess.run(['sleep', '2'])
# * run ping command
# subprocess.run(['ping', 'www.google.com'])
# * Return Error Message
result = subprocess.run(['ls', 'no_such_file_or_dir'])
print('error msg --->', result.returncode)
# * Capturing output
output_result = subprocess.run(['host', '8.8.8.8'], capture_output=True)
print(output_result)
print(output_result.returncode)
print(output_result.stdout.decode().split())
# * Capturing -> STDOUT and STDERR
result_2 = subprocess.run(['rm', 'no_such_file'], capture_output=True)
print(result_2.returncode)
print(result_2.stdout)
print(result_2.stderr)