Get all output from subprocess in python [duplicate]












0
















This question already has an answer here:




  • Running shell command and capturing the output

    14 answers




I'm using python 3.7 on Windows. I'm trying to execute a simple scan command and get its output as a string.
When I execute the command in python I only get the first line:



import subprocess

def execute(command):
proc = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
output = proc.stdout if proc.stdout else proc.stderr

path = "Somepath"
command = ['ecls.exe', '/files', path]
print(execute(command))



WARNING! The scanner was run in the account of a limited user.




But when I run it in the CMD:




ecls.exe /files "SomePath" WARNING! The scanner was run in the account
of a limited user.



ECLS Command-line scanner ...



Command line: /files SomePath



Scan started at: 11/24/18 14:18:11



Scan completed at: 11/24/18 14:18:11 Scan time: 0 sec
(0:00:00) Total: files - 1, objects 1 Infected:

files - 0, objects 0 Cleaned: files - 0, objects 0




I think that the command spawn a child process and it produces the scan output. I also tried to iterate over stdout but got the same output.



EDIT:
I tried other methods like check_output, Popen, etc with using PIPE but I only get the first line of output. I also tried to use shell=True but didn't make any difference. As I already said the command spawn a child process and I need to capture its output which seems that subprocess can't do it directly.










share|improve this question















marked as duplicate by stovfl, Jean-François Fabre python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 20:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • @stovfl It didn't work for me.

    – Masoud
    Nov 24 '18 at 15:25











  • Edit your Question and show what you have tried using the dup link Answer and where you get stuck.

    – stovfl
    Nov 24 '18 at 16:31
















0
















This question already has an answer here:




  • Running shell command and capturing the output

    14 answers




I'm using python 3.7 on Windows. I'm trying to execute a simple scan command and get its output as a string.
When I execute the command in python I only get the first line:



import subprocess

def execute(command):
proc = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
output = proc.stdout if proc.stdout else proc.stderr

path = "Somepath"
command = ['ecls.exe', '/files', path]
print(execute(command))



WARNING! The scanner was run in the account of a limited user.




But when I run it in the CMD:




ecls.exe /files "SomePath" WARNING! The scanner was run in the account
of a limited user.



ECLS Command-line scanner ...



Command line: /files SomePath



Scan started at: 11/24/18 14:18:11



Scan completed at: 11/24/18 14:18:11 Scan time: 0 sec
(0:00:00) Total: files - 1, objects 1 Infected:

files - 0, objects 0 Cleaned: files - 0, objects 0




I think that the command spawn a child process and it produces the scan output. I also tried to iterate over stdout but got the same output.



EDIT:
I tried other methods like check_output, Popen, etc with using PIPE but I only get the first line of output. I also tried to use shell=True but didn't make any difference. As I already said the command spawn a child process and I need to capture its output which seems that subprocess can't do it directly.










share|improve this question















marked as duplicate by stovfl, Jean-François Fabre python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 20:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • @stovfl It didn't work for me.

    – Masoud
    Nov 24 '18 at 15:25











  • Edit your Question and show what you have tried using the dup link Answer and where you get stuck.

    – stovfl
    Nov 24 '18 at 16:31














0












0








0









This question already has an answer here:




  • Running shell command and capturing the output

    14 answers




I'm using python 3.7 on Windows. I'm trying to execute a simple scan command and get its output as a string.
When I execute the command in python I only get the first line:



import subprocess

def execute(command):
proc = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
output = proc.stdout if proc.stdout else proc.stderr

path = "Somepath"
command = ['ecls.exe', '/files', path]
print(execute(command))



WARNING! The scanner was run in the account of a limited user.




But when I run it in the CMD:




ecls.exe /files "SomePath" WARNING! The scanner was run in the account
of a limited user.



ECLS Command-line scanner ...



Command line: /files SomePath



Scan started at: 11/24/18 14:18:11



Scan completed at: 11/24/18 14:18:11 Scan time: 0 sec
(0:00:00) Total: files - 1, objects 1 Infected:

files - 0, objects 0 Cleaned: files - 0, objects 0




I think that the command spawn a child process and it produces the scan output. I also tried to iterate over stdout but got the same output.



EDIT:
I tried other methods like check_output, Popen, etc with using PIPE but I only get the first line of output. I also tried to use shell=True but didn't make any difference. As I already said the command spawn a child process and I need to capture its output which seems that subprocess can't do it directly.










share|improve this question

















This question already has an answer here:




  • Running shell command and capturing the output

    14 answers




I'm using python 3.7 on Windows. I'm trying to execute a simple scan command and get its output as a string.
When I execute the command in python I only get the first line:



import subprocess

def execute(command):
proc = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
output = proc.stdout if proc.stdout else proc.stderr

path = "Somepath"
command = ['ecls.exe', '/files', path]
print(execute(command))



WARNING! The scanner was run in the account of a limited user.




But when I run it in the CMD:




ecls.exe /files "SomePath" WARNING! The scanner was run in the account
of a limited user.



ECLS Command-line scanner ...



Command line: /files SomePath



Scan started at: 11/24/18 14:18:11



Scan completed at: 11/24/18 14:18:11 Scan time: 0 sec
(0:00:00) Total: files - 1, objects 1 Infected:

files - 0, objects 0 Cleaned: files - 0, objects 0




I think that the command spawn a child process and it produces the scan output. I also tried to iterate over stdout but got the same output.



EDIT:
I tried other methods like check_output, Popen, etc with using PIPE but I only get the first line of output. I also tried to use shell=True but didn't make any difference. As I already said the command spawn a child process and I need to capture its output which seems that subprocess can't do it directly.





This question already has an answer here:




  • Running shell command and capturing the output

    14 answers








python python-3.x subprocess






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 9:55







Masoud

















asked Nov 24 '18 at 11:03









MasoudMasoud

5621627




5621627




marked as duplicate by stovfl, Jean-François Fabre python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 20:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by stovfl, Jean-François Fabre python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 20:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • @stovfl It didn't work for me.

    – Masoud
    Nov 24 '18 at 15:25











  • Edit your Question and show what you have tried using the dup link Answer and where you get stuck.

    – stovfl
    Nov 24 '18 at 16:31



















  • @stovfl It didn't work for me.

    – Masoud
    Nov 24 '18 at 15:25











  • Edit your Question and show what you have tried using the dup link Answer and where you get stuck.

    – stovfl
    Nov 24 '18 at 16:31

















@stovfl It didn't work for me.

– Masoud
Nov 24 '18 at 15:25





@stovfl It didn't work for me.

– Masoud
Nov 24 '18 at 15:25













Edit your Question and show what you have tried using the dup link Answer and where you get stuck.

– stovfl
Nov 24 '18 at 16:31





Edit your Question and show what you have tried using the dup link Answer and where you get stuck.

– stovfl
Nov 24 '18 at 16:31












1 Answer
1






active

oldest

votes


















1














As I couldn't find a direct way to solve this problem, with help of this reference, the output can be directed to a text file and then read it back.



import subprocess
import os
import tempfile

def execute_to_file(command):
"""
This function execute the command
and pass its output to a tempfile then read it back
It is usefull for process that deploy child process
"""
temp_file = tempfile.NamedTemporaryFile(delete=False)
temp_file.close()
path = temp_file.name
command = command + " > " + path
proc = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
if proc.stderr:
# if command failed return
os.unlink(path)
return
with open(path, 'r') as f:
data = f.read()
os.unlink(path)
return data

if __name__ == "__main__":
path = "Somepath"
command = 'ecls.exe /files ' + path
print(execute(command))





share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    As I couldn't find a direct way to solve this problem, with help of this reference, the output can be directed to a text file and then read it back.



    import subprocess
    import os
    import tempfile

    def execute_to_file(command):
    """
    This function execute the command
    and pass its output to a tempfile then read it back
    It is usefull for process that deploy child process
    """
    temp_file = tempfile.NamedTemporaryFile(delete=False)
    temp_file.close()
    path = temp_file.name
    command = command + " > " + path
    proc = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
    if proc.stderr:
    # if command failed return
    os.unlink(path)
    return
    with open(path, 'r') as f:
    data = f.read()
    os.unlink(path)
    return data

    if __name__ == "__main__":
    path = "Somepath"
    command = 'ecls.exe /files ' + path
    print(execute(command))





    share|improve this answer




























      1














      As I couldn't find a direct way to solve this problem, with help of this reference, the output can be directed to a text file and then read it back.



      import subprocess
      import os
      import tempfile

      def execute_to_file(command):
      """
      This function execute the command
      and pass its output to a tempfile then read it back
      It is usefull for process that deploy child process
      """
      temp_file = tempfile.NamedTemporaryFile(delete=False)
      temp_file.close()
      path = temp_file.name
      command = command + " > " + path
      proc = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
      if proc.stderr:
      # if command failed return
      os.unlink(path)
      return
      with open(path, 'r') as f:
      data = f.read()
      os.unlink(path)
      return data

      if __name__ == "__main__":
      path = "Somepath"
      command = 'ecls.exe /files ' + path
      print(execute(command))





      share|improve this answer


























        1












        1








        1







        As I couldn't find a direct way to solve this problem, with help of this reference, the output can be directed to a text file and then read it back.



        import subprocess
        import os
        import tempfile

        def execute_to_file(command):
        """
        This function execute the command
        and pass its output to a tempfile then read it back
        It is usefull for process that deploy child process
        """
        temp_file = tempfile.NamedTemporaryFile(delete=False)
        temp_file.close()
        path = temp_file.name
        command = command + " > " + path
        proc = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
        if proc.stderr:
        # if command failed return
        os.unlink(path)
        return
        with open(path, 'r') as f:
        data = f.read()
        os.unlink(path)
        return data

        if __name__ == "__main__":
        path = "Somepath"
        command = 'ecls.exe /files ' + path
        print(execute(command))





        share|improve this answer













        As I couldn't find a direct way to solve this problem, with help of this reference, the output can be directed to a text file and then read it back.



        import subprocess
        import os
        import tempfile

        def execute_to_file(command):
        """
        This function execute the command
        and pass its output to a tempfile then read it back
        It is usefull for process that deploy child process
        """
        temp_file = tempfile.NamedTemporaryFile(delete=False)
        temp_file.close()
        path = temp_file.name
        command = command + " > " + path
        proc = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
        if proc.stderr:
        # if command failed return
        os.unlink(path)
        return
        with open(path, 'r') as f:
        data = f.read()
        os.unlink(path)
        return data

        if __name__ == "__main__":
        path = "Somepath"
        command = 'ecls.exe /files ' + path
        print(execute(command))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 8:11









        MasoudMasoud

        5621627




        5621627

















            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'