-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[sonic-py-common] Add getstatusoutput_noshell() functions to general module #12065
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6a6dd70
Add getstatusoutput_noshell functions
maipbui f7d90d0
Fix getstatusoutput and add check_output_pipe
maipbui 0e8f4ce
Fix PR commends and add UT
maipbui 8e3b376
Add ref
maipbui 5b256df
Modify code for python 2
maipbui bfd3862
Modify UT to pass py2
maipbui 38ca258
Return p1.stdout in exception 1
maipbui fbe43bf
Extend 2 functions to handle more pipe commands
maipbui 99ccf28
Fix PR comments
maipbui cd007de
Fix comments
maipbui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import sys | ||
import subprocess | ||
|
||
|
||
def load_module_from_source(module_name, file_path): | ||
|
@@ -23,3 +24,33 @@ def load_module_from_source(module_name, file_path): | |
sys.modules[module_name] = module | ||
|
||
return module | ||
|
||
|
||
def getstatusoutput_noshell(cmd): | ||
""" | ||
This function implements getstatusoutput API from subprocess module | ||
but using shell=False to prevent shell injection. | ||
""" | ||
p = subprocess.run(cmd, capture_output=True, universal_newlines=True) | ||
status, output = p.returncode, p.stdout | ||
if output[-1:] == '\n': | ||
output = output[:-1] | ||
return (status, output) | ||
|
||
|
||
def getstatusoutput_noshell_pipe(cmd1, cmd2): | ||
""" | ||
This function implements getstatusoutput API from subprocess module | ||
but using shell=False to prevent shell injection. Input command includes | ||
pipe command. | ||
""" | ||
status = 0 | ||
with subprocess.Popen(cmd1, universal_newlines=True, stdout=subprocess.PIPE) as p1: | ||
with subprocess.Popen(cmd2, universal_newlines=True, stdin=p1.stdout, stdout=subprocess.PIPE) as p2: | ||
output = p2.communicate()[0] | ||
p1.wait() | ||
if p1.returncode != 0 and p2.returncode != 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
status = 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if output[-1:] == '\n': | ||
output = output[:-1] | ||
return (status, output) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add unit tests to cover new functions. #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, working on UT