V
V
Vadim kyklaed2019-03-05 22:12:05
linux
Vadim kyklaed, 2019-03-05 22:12:05

How to use subprocess to pass the password from sudo but not make the process blocked?

Hello, tell me, is there such a tcpdump tool,
I need to run it at a certain moment so that it starts running and the logs are written to a file. the console output is not needed, but it requires sudo password
here is what I did:

import subprocess
import signal

command = ['sudo', '-S','tcpdump', '-w', '/home/user/dump.pcap']
output = subprocess.Popen(command, stdout=subprocess.PIPE, stderr = subprocess.STDOUT, encoding='utf8' )
out,data = output.communicate('password'+'\n')

It doesn't quite work, I pass the password and the execution starts but the communication blocks and I can't complete the work at the moment when I need it, and the timer exit is not good (you can catch the exception),
How to do it right? I need to start the execution and at some point, maybe after 5 seconds, or maybe somehow else pass the code to stop and exit.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2019-03-05
@kyklaed

it requires sudo password

It's not "she requires", it's your sudo set up that way. Set it up so that it doesn't require a password (at all, or just to run tcpdump) -- and you'll have one less problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question