The problem is that you are using shell redirection in a command, but when using a subprocess, the shell is not generated.
Consider the following (very simple) program:
import sys print sys.argv
Now, if we run it as if you were using ssh (suppose foofile.txt exists), we get:
python argcheck.py ssh cat < foofile.txt " | /path/to/bash/script.sh;" ['argcheck.py', 'ssh', 'cat', ' | /path/to/bash/script.sh;']
Note that < foofile.txt never gets into python command line arguments. This is because the bash parser intercepts < and the file that appears after it and redirects the contents of this file to your stdin program. In other words, ssh reads the file from stdin. You want your file to be transferred to stdin from ssh , using python as well.
s = """ ssh xxxx cat '| /path/to/bash/script.sh;' """ #<snip> proc=subprocess.Popen(sbash,stdout=subprocess.PIPE,stderr=subprocess.PIPE, stdin=subprocess.PIPE) out,err=proc.communicate(open(fn).read())
will work presumably.
The following works for me:
import subprocess from subprocess import PIPE with open('foo.h') as f: p = subprocess.Popen(['ssh',' mgilson@XXXXX ','cat','| cat'],stdin=f,stdout=PIPE,stderr=PIPE) out,err = p.communicate() print out print '#'*80 print err
And the equivalent command in bash :
ssh mgilson@XXXXX cat < foo.h '| cat'
where foo.h is the file on my local machine.