-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_process.py
More file actions
45 lines (36 loc) · 1.41 KB
/
command_process.py
File metadata and controls
45 lines (36 loc) · 1.41 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
#!/usr/bin/env python3.9
import subprocess
import sys
import os
#Example python code for executing commands and returning clean stdout
#Take input and stuff in command input
command_input=input("Command to run\n")
process=(str(subprocess.run(command_input.split(), capture_output=True)))
"""
#######################################
command_input.split()
-will break apart the command_input into a list for subprocess.run() using spaces as delimiters
subprocess.run('ls','-l', '/', capture_output=True) -
-would list the root directory and return the output
#######################################
"""
left="stdout=b\'"
right="\', stderr=b"
pre_return=(str(process[process.index(left)+len(left):process.index(right)]))
clean_return=pre_return.replace('\\n','\n').replace('\\t','\t')
print(clean_return)
"""
#######################################
left="stdout=b\'"
-this is the left boundary of stdout output from subprocess.run()
right="\', stderr=b"
-this is the right boundary of stdout output from subprocess.run()
pre_return=(str(process[process.index(left)+len(left):process.index(right)]))
-this extracts the stdout from using the limits left and right
clean_return=pre_return.replace('\\n','\n').replace('\\t','\t')
-this fixes the newline characters from our output
print(clean_return)
-prints the clean return
#######################################
"""
#Thats it -- pretty basic