Calling a command line utility from Python












1















I am currently trying to utilize strace to automatically trace a programm 's system calls. To then parse and process the data obtained, I want to use a Python script.



I now wonder, how would I go about calling strace from Python?
Strace is usually called via command line and I don't know of any C library compiled from strace which I could utilize.



What is the general way to simulate an access via command line via Python?
alternatively: are there any tools similar to strace written natively in Python?



I'm thankful for any kind of help.



Nothing, as I'm clueless










share|improve this question



























    1















    I am currently trying to utilize strace to automatically trace a programm 's system calls. To then parse and process the data obtained, I want to use a Python script.



    I now wonder, how would I go about calling strace from Python?
    Strace is usually called via command line and I don't know of any C library compiled from strace which I could utilize.



    What is the general way to simulate an access via command line via Python?
    alternatively: are there any tools similar to strace written natively in Python?



    I'm thankful for any kind of help.



    Nothing, as I'm clueless










    share|improve this question

























      1












      1








      1








      I am currently trying to utilize strace to automatically trace a programm 's system calls. To then parse and process the data obtained, I want to use a Python script.



      I now wonder, how would I go about calling strace from Python?
      Strace is usually called via command line and I don't know of any C library compiled from strace which I could utilize.



      What is the general way to simulate an access via command line via Python?
      alternatively: are there any tools similar to strace written natively in Python?



      I'm thankful for any kind of help.



      Nothing, as I'm clueless










      share|improve this question














      I am currently trying to utilize strace to automatically trace a programm 's system calls. To then parse and process the data obtained, I want to use a Python script.



      I now wonder, how would I go about calling strace from Python?
      Strace is usually called via command line and I don't know of any C library compiled from strace which I could utilize.



      What is the general way to simulate an access via command line via Python?
      alternatively: are there any tools similar to strace written natively in Python?



      I'm thankful for any kind of help.



      Nothing, as I'm clueless







      python terminal debian strace






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 31 '18 at 14:50









      Daniel SiegelDaniel Siegel

      1081211




      1081211
























          3 Answers
          3






          active

          oldest

          votes


















          2














          You need to use the subprocess module.



          It has check_output to read the output and put it in a variable, and check_call to just check the exit code.



          If you want to run a shell script you can write it all in a string and set shell=True, otherwise just put the parameters as strings in a list.



          import subprocess
          # Single process
          subprocess.check_output(['fortune', '-m', 'ciao'])
          # Run it in a shell
          subprocess.check_output('fortune | grep a', shell=True)


          Remember that if you run stuff in a shell, if you don't escape properly and allow user data to go in your string, it's easy to make security holes. It is better to not use shell=True.






          share|improve this answer































            1














            You can use commands as the following:



            import commands
            cmd = "strace command"
            result = commands.getstatusoutput(cmd)
            if result[0] == 0:
            print result[1]
            else:
            print "Something went wrong executing your command"


            result[0] contains the return code, and result[1] contains the output.






            share|improve this answer


























            • ModuleNotFoundError: No module named 'commands'

              – LtWorf
              Dec 31 '18 at 15:08











            • You need to install it: pip install commands

              – Walid Da.
              Dec 31 '18 at 15:11











            • You know python has a subprocess module and there is absolutely no need to depend on other libraries?

              – LtWorf
              Dec 31 '18 at 15:13











            • subprocess.check_output('fortune | grep a', shell=True) there, no more dependency to your useless module.

              – LtWorf
              Dec 31 '18 at 15:14



















            0














            Python 2 and Python 3 (prior 3.5)



            Simply execute:



            subprocess.call(["strace", "command"])


            Execute and return the output for processing:



            output = subprocess.check_output(["strace", "command"])


            Reference: https://docs.python.org/2/library/subprocess.html



            Python 3.5+



            output = subprocess.run(["strace", "command"], caputure_output=True)


            Reference: https://docs.python.org/3.7/library/subprocess.html#subprocess.run






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988715%2fcalling-a-command-line-utility-from-python%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              You need to use the subprocess module.



              It has check_output to read the output and put it in a variable, and check_call to just check the exit code.



              If you want to run a shell script you can write it all in a string and set shell=True, otherwise just put the parameters as strings in a list.



              import subprocess
              # Single process
              subprocess.check_output(['fortune', '-m', 'ciao'])
              # Run it in a shell
              subprocess.check_output('fortune | grep a', shell=True)


              Remember that if you run stuff in a shell, if you don't escape properly and allow user data to go in your string, it's easy to make security holes. It is better to not use shell=True.






              share|improve this answer




























                2














                You need to use the subprocess module.



                It has check_output to read the output and put it in a variable, and check_call to just check the exit code.



                If you want to run a shell script you can write it all in a string and set shell=True, otherwise just put the parameters as strings in a list.



                import subprocess
                # Single process
                subprocess.check_output(['fortune', '-m', 'ciao'])
                # Run it in a shell
                subprocess.check_output('fortune | grep a', shell=True)


                Remember that if you run stuff in a shell, if you don't escape properly and allow user data to go in your string, it's easy to make security holes. It is better to not use shell=True.






                share|improve this answer


























                  2












                  2








                  2







                  You need to use the subprocess module.



                  It has check_output to read the output and put it in a variable, and check_call to just check the exit code.



                  If you want to run a shell script you can write it all in a string and set shell=True, otherwise just put the parameters as strings in a list.



                  import subprocess
                  # Single process
                  subprocess.check_output(['fortune', '-m', 'ciao'])
                  # Run it in a shell
                  subprocess.check_output('fortune | grep a', shell=True)


                  Remember that if you run stuff in a shell, if you don't escape properly and allow user data to go in your string, it's easy to make security holes. It is better to not use shell=True.






                  share|improve this answer













                  You need to use the subprocess module.



                  It has check_output to read the output and put it in a variable, and check_call to just check the exit code.



                  If you want to run a shell script you can write it all in a string and set shell=True, otherwise just put the parameters as strings in a list.



                  import subprocess
                  # Single process
                  subprocess.check_output(['fortune', '-m', 'ciao'])
                  # Run it in a shell
                  subprocess.check_output('fortune | grep a', shell=True)


                  Remember that if you run stuff in a shell, if you don't escape properly and allow user data to go in your string, it's easy to make security holes. It is better to not use shell=True.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 31 '18 at 15:18









                  LtWorfLtWorf

                  4,30622033




                  4,30622033

























                      1














                      You can use commands as the following:



                      import commands
                      cmd = "strace command"
                      result = commands.getstatusoutput(cmd)
                      if result[0] == 0:
                      print result[1]
                      else:
                      print "Something went wrong executing your command"


                      result[0] contains the return code, and result[1] contains the output.






                      share|improve this answer


























                      • ModuleNotFoundError: No module named 'commands'

                        – LtWorf
                        Dec 31 '18 at 15:08











                      • You need to install it: pip install commands

                        – Walid Da.
                        Dec 31 '18 at 15:11











                      • You know python has a subprocess module and there is absolutely no need to depend on other libraries?

                        – LtWorf
                        Dec 31 '18 at 15:13











                      • subprocess.check_output('fortune | grep a', shell=True) there, no more dependency to your useless module.

                        – LtWorf
                        Dec 31 '18 at 15:14
















                      1














                      You can use commands as the following:



                      import commands
                      cmd = "strace command"
                      result = commands.getstatusoutput(cmd)
                      if result[0] == 0:
                      print result[1]
                      else:
                      print "Something went wrong executing your command"


                      result[0] contains the return code, and result[1] contains the output.






                      share|improve this answer


























                      • ModuleNotFoundError: No module named 'commands'

                        – LtWorf
                        Dec 31 '18 at 15:08











                      • You need to install it: pip install commands

                        – Walid Da.
                        Dec 31 '18 at 15:11











                      • You know python has a subprocess module and there is absolutely no need to depend on other libraries?

                        – LtWorf
                        Dec 31 '18 at 15:13











                      • subprocess.check_output('fortune | grep a', shell=True) there, no more dependency to your useless module.

                        – LtWorf
                        Dec 31 '18 at 15:14














                      1












                      1








                      1







                      You can use commands as the following:



                      import commands
                      cmd = "strace command"
                      result = commands.getstatusoutput(cmd)
                      if result[0] == 0:
                      print result[1]
                      else:
                      print "Something went wrong executing your command"


                      result[0] contains the return code, and result[1] contains the output.






                      share|improve this answer















                      You can use commands as the following:



                      import commands
                      cmd = "strace command"
                      result = commands.getstatusoutput(cmd)
                      if result[0] == 0:
                      print result[1]
                      else:
                      print "Something went wrong executing your command"


                      result[0] contains the return code, and result[1] contains the output.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 31 '18 at 14:58









                      MarianD

                      4,33261331




                      4,33261331










                      answered Dec 31 '18 at 14:52









                      Walid Da.Walid Da.

                      6141413




                      6141413













                      • ModuleNotFoundError: No module named 'commands'

                        – LtWorf
                        Dec 31 '18 at 15:08











                      • You need to install it: pip install commands

                        – Walid Da.
                        Dec 31 '18 at 15:11











                      • You know python has a subprocess module and there is absolutely no need to depend on other libraries?

                        – LtWorf
                        Dec 31 '18 at 15:13











                      • subprocess.check_output('fortune | grep a', shell=True) there, no more dependency to your useless module.

                        – LtWorf
                        Dec 31 '18 at 15:14



















                      • ModuleNotFoundError: No module named 'commands'

                        – LtWorf
                        Dec 31 '18 at 15:08











                      • You need to install it: pip install commands

                        – Walid Da.
                        Dec 31 '18 at 15:11











                      • You know python has a subprocess module and there is absolutely no need to depend on other libraries?

                        – LtWorf
                        Dec 31 '18 at 15:13











                      • subprocess.check_output('fortune | grep a', shell=True) there, no more dependency to your useless module.

                        – LtWorf
                        Dec 31 '18 at 15:14

















                      ModuleNotFoundError: No module named 'commands'

                      – LtWorf
                      Dec 31 '18 at 15:08





                      ModuleNotFoundError: No module named 'commands'

                      – LtWorf
                      Dec 31 '18 at 15:08













                      You need to install it: pip install commands

                      – Walid Da.
                      Dec 31 '18 at 15:11





                      You need to install it: pip install commands

                      – Walid Da.
                      Dec 31 '18 at 15:11













                      You know python has a subprocess module and there is absolutely no need to depend on other libraries?

                      – LtWorf
                      Dec 31 '18 at 15:13





                      You know python has a subprocess module and there is absolutely no need to depend on other libraries?

                      – LtWorf
                      Dec 31 '18 at 15:13













                      subprocess.check_output('fortune | grep a', shell=True) there, no more dependency to your useless module.

                      – LtWorf
                      Dec 31 '18 at 15:14





                      subprocess.check_output('fortune | grep a', shell=True) there, no more dependency to your useless module.

                      – LtWorf
                      Dec 31 '18 at 15:14











                      0














                      Python 2 and Python 3 (prior 3.5)



                      Simply execute:



                      subprocess.call(["strace", "command"])


                      Execute and return the output for processing:



                      output = subprocess.check_output(["strace", "command"])


                      Reference: https://docs.python.org/2/library/subprocess.html



                      Python 3.5+



                      output = subprocess.run(["strace", "command"], caputure_output=True)


                      Reference: https://docs.python.org/3.7/library/subprocess.html#subprocess.run






                      share|improve this answer




























                        0














                        Python 2 and Python 3 (prior 3.5)



                        Simply execute:



                        subprocess.call(["strace", "command"])


                        Execute and return the output for processing:



                        output = subprocess.check_output(["strace", "command"])


                        Reference: https://docs.python.org/2/library/subprocess.html



                        Python 3.5+



                        output = subprocess.run(["strace", "command"], caputure_output=True)


                        Reference: https://docs.python.org/3.7/library/subprocess.html#subprocess.run






                        share|improve this answer


























                          0












                          0








                          0







                          Python 2 and Python 3 (prior 3.5)



                          Simply execute:



                          subprocess.call(["strace", "command"])


                          Execute and return the output for processing:



                          output = subprocess.check_output(["strace", "command"])


                          Reference: https://docs.python.org/2/library/subprocess.html



                          Python 3.5+



                          output = subprocess.run(["strace", "command"], caputure_output=True)


                          Reference: https://docs.python.org/3.7/library/subprocess.html#subprocess.run






                          share|improve this answer













                          Python 2 and Python 3 (prior 3.5)



                          Simply execute:



                          subprocess.call(["strace", "command"])


                          Execute and return the output for processing:



                          output = subprocess.check_output(["strace", "command"])


                          Reference: https://docs.python.org/2/library/subprocess.html



                          Python 3.5+



                          output = subprocess.run(["strace", "command"], caputure_output=True)


                          Reference: https://docs.python.org/3.7/library/subprocess.html#subprocess.run







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 31 '18 at 15:32









                          TaiTai

                          616




                          616






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988715%2fcalling-a-command-line-utility-from-python%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Mossoró

                              Error while reading .h5 file using the rhdf5 package in R

                              Pushsharp Apns notification error: 'InvalidToken'