Read the output of another program with python2019 Community Moderator ElectionRetrieving the output of subprocess.call()Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How do I get time of a Python program's execution?Does Python have a string 'contains' substring method?How to print the full traceback without halting the program?Why is reading lines from stdin much slower in C++ than Python?

Maths symbols and unicode-math input inside siunitx commands

Have the tides ever turned twice on any open problem?

Can you move over difficult terrain with only 5 feet of movement?

gerund and noun applications

HP P840 HDD RAID 5 many strange drive failures

Does the attack bonus from a Masterwork weapon stack with the attack bonus from Masterwork ammunition?

Light propagating through a sound wave

Is it insecure to send a password in a `curl` command?

What are substitutions for coconut in curry?

Usage and meaning of "up" in "...worth at least a thousand pounds up in London"

Can a wizard cast a spell during their first turn of combat if they initiated combat by releasing a readied spell?

Worshiping one God at a time?

Matrix using tikz package

Do I need to be arrogant to get ahead?

Optimising a list searching algorithm

What favor did Moody owe Dumbledore?

Turning a hard to access nut?

Print a physical multiplication table

Calculate the frequency of characters in a string

Probably overheated black color SMD pads

Unfrosted light bulb

Bash - pair each line of file

What is the term when voters “dishonestly” choose something that they do not want to choose?

Knife as defense against stray dogs



Read the output of another program with python



2019 Community Moderator ElectionRetrieving the output of subprocess.call()Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How do I get time of a Python program's execution?Does Python have a string 'contains' substring method?How to print the full traceback without halting the program?Why is reading lines from stdin much slower in C++ than Python?










-1















So lets say I open CMD or PowerShell and enter



ping google.com -t


I will get a constant stream of pings. (I could obviously also write a script that gets pings within python but that is not the problem, I just want to know how to do this in theory.) What i want to do now is write a python script that gets those outputs from the CMD and prints them out, live. Is this even possible?



Example: I am running a server which prints logs into a command line and I want to read and save them (i know the save part, just the retrieve is my problem)



Example2: I am running my monero wallet (not the gui, the command line version) and want a script that notifies me when money is sent in



Summed up: an external program of which the UI is a Command line or whose logs are printed into CMD or similar programs is running, and i want to get all the things this program prints out.










share|improve this question



















  • 1





    Possible duplicate of Retrieving the output of subprocess.call()

    – SiHa
    Mar 7 at 16:55











  • @SiHa as far as i understand it I need to call the program from within python for it to work. If possible I just want to enter the PID from task manager and read the output of an already running program which is running in the command line (like the command line monero wallet) or something like that

    – Maritn Ge
    Mar 7 at 17:04











  • What operating system?

    – MarkReedZ
    Mar 7 at 17:13











  • @MarkReedZ Windows for now, I will later on also need Ubuntu, but I think once I know how it works on one I can figure the rest out (if you know linux though you can also tell me)

    – Maritn Ge
    Mar 7 at 17:16















-1















So lets say I open CMD or PowerShell and enter



ping google.com -t


I will get a constant stream of pings. (I could obviously also write a script that gets pings within python but that is not the problem, I just want to know how to do this in theory.) What i want to do now is write a python script that gets those outputs from the CMD and prints them out, live. Is this even possible?



Example: I am running a server which prints logs into a command line and I want to read and save them (i know the save part, just the retrieve is my problem)



Example2: I am running my monero wallet (not the gui, the command line version) and want a script that notifies me when money is sent in



Summed up: an external program of which the UI is a Command line or whose logs are printed into CMD or similar programs is running, and i want to get all the things this program prints out.










share|improve this question



















  • 1





    Possible duplicate of Retrieving the output of subprocess.call()

    – SiHa
    Mar 7 at 16:55











  • @SiHa as far as i understand it I need to call the program from within python for it to work. If possible I just want to enter the PID from task manager and read the output of an already running program which is running in the command line (like the command line monero wallet) or something like that

    – Maritn Ge
    Mar 7 at 17:04











  • What operating system?

    – MarkReedZ
    Mar 7 at 17:13











  • @MarkReedZ Windows for now, I will later on also need Ubuntu, but I think once I know how it works on one I can figure the rest out (if you know linux though you can also tell me)

    – Maritn Ge
    Mar 7 at 17:16













-1












-1








-1


2






So lets say I open CMD or PowerShell and enter



ping google.com -t


I will get a constant stream of pings. (I could obviously also write a script that gets pings within python but that is not the problem, I just want to know how to do this in theory.) What i want to do now is write a python script that gets those outputs from the CMD and prints them out, live. Is this even possible?



Example: I am running a server which prints logs into a command line and I want to read and save them (i know the save part, just the retrieve is my problem)



Example2: I am running my monero wallet (not the gui, the command line version) and want a script that notifies me when money is sent in



Summed up: an external program of which the UI is a Command line or whose logs are printed into CMD or similar programs is running, and i want to get all the things this program prints out.










share|improve this question
















So lets say I open CMD or PowerShell and enter



ping google.com -t


I will get a constant stream of pings. (I could obviously also write a script that gets pings within python but that is not the problem, I just want to know how to do this in theory.) What i want to do now is write a python script that gets those outputs from the CMD and prints them out, live. Is this even possible?



Example: I am running a server which prints logs into a command line and I want to read and save them (i know the save part, just the retrieve is my problem)



Example2: I am running my monero wallet (not the gui, the command line version) and want a script that notifies me when money is sent in



Summed up: an external program of which the UI is a Command line or whose logs are printed into CMD or similar programs is running, and i want to get all the things this program prints out.







python python-3.x cmd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 17:12







Maritn Ge

















asked Mar 7 at 16:11









Maritn GeMaritn Ge

168




168







  • 1





    Possible duplicate of Retrieving the output of subprocess.call()

    – SiHa
    Mar 7 at 16:55











  • @SiHa as far as i understand it I need to call the program from within python for it to work. If possible I just want to enter the PID from task manager and read the output of an already running program which is running in the command line (like the command line monero wallet) or something like that

    – Maritn Ge
    Mar 7 at 17:04











  • What operating system?

    – MarkReedZ
    Mar 7 at 17:13











  • @MarkReedZ Windows for now, I will later on also need Ubuntu, but I think once I know how it works on one I can figure the rest out (if you know linux though you can also tell me)

    – Maritn Ge
    Mar 7 at 17:16












  • 1





    Possible duplicate of Retrieving the output of subprocess.call()

    – SiHa
    Mar 7 at 16:55











  • @SiHa as far as i understand it I need to call the program from within python for it to work. If possible I just want to enter the PID from task manager and read the output of an already running program which is running in the command line (like the command line monero wallet) or something like that

    – Maritn Ge
    Mar 7 at 17:04











  • What operating system?

    – MarkReedZ
    Mar 7 at 17:13











  • @MarkReedZ Windows for now, I will later on also need Ubuntu, but I think once I know how it works on one I can figure the rest out (if you know linux though you can also tell me)

    – Maritn Ge
    Mar 7 at 17:16







1




1





Possible duplicate of Retrieving the output of subprocess.call()

– SiHa
Mar 7 at 16:55





Possible duplicate of Retrieving the output of subprocess.call()

– SiHa
Mar 7 at 16:55













@SiHa as far as i understand it I need to call the program from within python for it to work. If possible I just want to enter the PID from task manager and read the output of an already running program which is running in the command line (like the command line monero wallet) or something like that

– Maritn Ge
Mar 7 at 17:04





@SiHa as far as i understand it I need to call the program from within python for it to work. If possible I just want to enter the PID from task manager and read the output of an already running program which is running in the command line (like the command line monero wallet) or something like that

– Maritn Ge
Mar 7 at 17:04













What operating system?

– MarkReedZ
Mar 7 at 17:13





What operating system?

– MarkReedZ
Mar 7 at 17:13













@MarkReedZ Windows for now, I will later on also need Ubuntu, but I think once I know how it works on one I can figure the rest out (if you know linux though you can also tell me)

– Maritn Ge
Mar 7 at 17:16





@MarkReedZ Windows for now, I will later on also need Ubuntu, but I think once I know how it works on one I can figure the rest out (if you know linux though you can also tell me)

– Maritn Ge
Mar 7 at 17:16












1 Answer
1






active

oldest

votes


















3














On linux this is the simplest way. 9590 is the pid of the process and -s says write out 500 characters from each output line (otherwise it truncates).



import os
stream = os.popen("strace -ewrite -p 9590 -s 500")
while 1:
print( stream.readline() )


You need to use sudo with strace



sudo python grab_output.py


Output with ping google is:



write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=125 ttl=44 time=40.3 msn", 82) = 82
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=126 ttl=44 time=32.5 msn", 82) = 82


So you'd have to clean it up a bit.






share|improve this answer

























  • Is the following what you want or are you asking to run the ping outside of python and attach to that process's output?

    – MarkReedZ
    Mar 7 at 16:56











  • the latter i think. run ping in cmd without python and the tell python to read the output of that command line

    – Maritn Ge
    Mar 7 at 17:18











  • I've updated the answer. What you are asking to do is "Use python to attach to the stdout of a running process". Using a debugger like gdb would also work.

    – MarkReedZ
    Mar 7 at 17:18











  • Perfect, thanks. Is there a similar way to do it on Windows? I need both but I think linux will suffice for now. Anyway, thank you!

    – Maritn Ge
    Mar 7 at 17:21










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%2f55048245%2fread-the-output-of-another-program-with-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














On linux this is the simplest way. 9590 is the pid of the process and -s says write out 500 characters from each output line (otherwise it truncates).



import os
stream = os.popen("strace -ewrite -p 9590 -s 500")
while 1:
print( stream.readline() )


You need to use sudo with strace



sudo python grab_output.py


Output with ping google is:



write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=125 ttl=44 time=40.3 msn", 82) = 82
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=126 ttl=44 time=32.5 msn", 82) = 82


So you'd have to clean it up a bit.






share|improve this answer

























  • Is the following what you want or are you asking to run the ping outside of python and attach to that process's output?

    – MarkReedZ
    Mar 7 at 16:56











  • the latter i think. run ping in cmd without python and the tell python to read the output of that command line

    – Maritn Ge
    Mar 7 at 17:18











  • I've updated the answer. What you are asking to do is "Use python to attach to the stdout of a running process". Using a debugger like gdb would also work.

    – MarkReedZ
    Mar 7 at 17:18











  • Perfect, thanks. Is there a similar way to do it on Windows? I need both but I think linux will suffice for now. Anyway, thank you!

    – Maritn Ge
    Mar 7 at 17:21















3














On linux this is the simplest way. 9590 is the pid of the process and -s says write out 500 characters from each output line (otherwise it truncates).



import os
stream = os.popen("strace -ewrite -p 9590 -s 500")
while 1:
print( stream.readline() )


You need to use sudo with strace



sudo python grab_output.py


Output with ping google is:



write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=125 ttl=44 time=40.3 msn", 82) = 82
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=126 ttl=44 time=32.5 msn", 82) = 82


So you'd have to clean it up a bit.






share|improve this answer

























  • Is the following what you want or are you asking to run the ping outside of python and attach to that process's output?

    – MarkReedZ
    Mar 7 at 16:56











  • the latter i think. run ping in cmd without python and the tell python to read the output of that command line

    – Maritn Ge
    Mar 7 at 17:18











  • I've updated the answer. What you are asking to do is "Use python to attach to the stdout of a running process". Using a debugger like gdb would also work.

    – MarkReedZ
    Mar 7 at 17:18











  • Perfect, thanks. Is there a similar way to do it on Windows? I need both but I think linux will suffice for now. Anyway, thank you!

    – Maritn Ge
    Mar 7 at 17:21













3












3








3







On linux this is the simplest way. 9590 is the pid of the process and -s says write out 500 characters from each output line (otherwise it truncates).



import os
stream = os.popen("strace -ewrite -p 9590 -s 500")
while 1:
print( stream.readline() )


You need to use sudo with strace



sudo python grab_output.py


Output with ping google is:



write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=125 ttl=44 time=40.3 msn", 82) = 82
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=126 ttl=44 time=32.5 msn", 82) = 82


So you'd have to clean it up a bit.






share|improve this answer















On linux this is the simplest way. 9590 is the pid of the process and -s says write out 500 characters from each output line (otherwise it truncates).



import os
stream = os.popen("strace -ewrite -p 9590 -s 500")
while 1:
print( stream.readline() )


You need to use sudo with strace



sudo python grab_output.py


Output with ping google is:



write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=125 ttl=44 time=40.3 msn", 82) = 82
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=126 ttl=44 time=32.5 msn", 82) = 82


So you'd have to clean it up a bit.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 7 at 17:15

























answered Mar 7 at 16:53









MarkReedZMarkReedZ

1,0097




1,0097












  • Is the following what you want or are you asking to run the ping outside of python and attach to that process's output?

    – MarkReedZ
    Mar 7 at 16:56











  • the latter i think. run ping in cmd without python and the tell python to read the output of that command line

    – Maritn Ge
    Mar 7 at 17:18











  • I've updated the answer. What you are asking to do is "Use python to attach to the stdout of a running process". Using a debugger like gdb would also work.

    – MarkReedZ
    Mar 7 at 17:18











  • Perfect, thanks. Is there a similar way to do it on Windows? I need both but I think linux will suffice for now. Anyway, thank you!

    – Maritn Ge
    Mar 7 at 17:21

















  • Is the following what you want or are you asking to run the ping outside of python and attach to that process's output?

    – MarkReedZ
    Mar 7 at 16:56











  • the latter i think. run ping in cmd without python and the tell python to read the output of that command line

    – Maritn Ge
    Mar 7 at 17:18











  • I've updated the answer. What you are asking to do is "Use python to attach to the stdout of a running process". Using a debugger like gdb would also work.

    – MarkReedZ
    Mar 7 at 17:18











  • Perfect, thanks. Is there a similar way to do it on Windows? I need both but I think linux will suffice for now. Anyway, thank you!

    – Maritn Ge
    Mar 7 at 17:21
















Is the following what you want or are you asking to run the ping outside of python and attach to that process's output?

– MarkReedZ
Mar 7 at 16:56





Is the following what you want or are you asking to run the ping outside of python and attach to that process's output?

– MarkReedZ
Mar 7 at 16:56













the latter i think. run ping in cmd without python and the tell python to read the output of that command line

– Maritn Ge
Mar 7 at 17:18





the latter i think. run ping in cmd without python and the tell python to read the output of that command line

– Maritn Ge
Mar 7 at 17:18













I've updated the answer. What you are asking to do is "Use python to attach to the stdout of a running process". Using a debugger like gdb would also work.

– MarkReedZ
Mar 7 at 17:18





I've updated the answer. What you are asking to do is "Use python to attach to the stdout of a running process". Using a debugger like gdb would also work.

– MarkReedZ
Mar 7 at 17:18













Perfect, thanks. Is there a similar way to do it on Windows? I need both but I think linux will suffice for now. Anyway, thank you!

– Maritn Ge
Mar 7 at 17:21





Perfect, thanks. Is there a similar way to do it on Windows? I need both but I think linux will suffice for now. Anyway, thank you!

– Maritn Ge
Mar 7 at 17:21



















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%2f55048245%2fread-the-output-of-another-program-with-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

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme