Python piping output between two subprocesses

I am working on some code that will be a DD block device via SSH, and I want to do this using a subprocess so that I can track the status of DD during transmission (by killing the dd process with SIGUSR1 to get its current state and reading using selects )

The command I'm trying to implement will be something like this:

dd if=/dev/sda | ssh root@example.com 'dd of=/dev/sda' 

The current method I tried was:

 dd_process = subprocess.Popen(['dd','if=/dev/sda'],0,None,None,subprocess.PIPE, subprocess.PIPE) ssh_process = subprocess.Popen(['ssh','root@example.com','dd of=/dev/sda'],0,None,dd_process.stdout) 

However, when I run this, the SSH process stops functioning after 10-40 seconds.
Am I completely dumb here or is there no way to lay between subprocesses?

Edit: Turns out my real code didn't have a hostname. This is the right way to do something.

+8
python unix subprocess
source share
2 answers
 from subprocess import Popen, PIPE dd_process = Popen(['dd', 'if=/dev/sda'], stdout=PIPE) ssh_process = Popen(['ssh', 'root@example.com', 'dd','of=/dev/sda'],stdin=dd_process.stdout, stdout=PIPE) dd_process.stdout.close() # enable write error in dd if ssh dies out, err = ssh_process.communicate() 

This is a way to output the first output of the process to the second. (note the stdin in ssh_process)

+13
source share

sh The Python library makes it easy to call OS commands and translate them.

From the documentation:

 for line in tr(tail("-f", "test.log", _piped=True), "[:upper:]", "[:lower:]", _iter=True): print(line) 
+4
source share

All Articles