0

I have this code here

import subprocess
from time import strftime       # Load just the strftime Module from Time

f = open('check_'+strftime("%Y-%m-%d")+'.log', 'w')
for server in open('check.txt'):
    f.write(server.strip() + "\n")
    subprocess.Popen(['plink', server.strip(), 'df','-k'],stdout=f)

What I would like to have is the output so it shows:

Server Name

Output

Server Name

Output

Currently it shows:

server name

server name

output

output

Thanks in advance

1 Answer 1

2

First of all - if your intention is to run different commands in remote servers, take a look at Fabric.

By default subprocess.Popen runs in the background, and your command is probably much slower than the other things in your loop (like printing the server names). Try this to force it to wait for each process:

import subprocess
from time import strftime       # Load just the strftime Module from Time

f = open('check_'+strftime("%Y-%m-%d")+'.log', 'w')
for server in open('check.txt'):
    f.write(server.strip() + "\n")
    p = subprocess.Popen(['plink', server.strip(), 'df','-k'],stdout=f)
    p.wait()
    f.flush()

If you intend to make the process run in parallel, I'd just write each server log to a different file. Then concatenate the result if needed.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, thanks very much for that, it solved it. I changed the f.flush() and p.wait() round as it was printing the server after the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.