I'm trying to run a set of commands through ssh from a Python script. I came upon the here-document
concept and thought: cool, let me implement something like this:
command = ( ( 'ssh user@host /usr/bin/bash <<EOF\n'
+ 'cd %s \n'
+ 'qsub %s\n'
+ 'EOF' ) % (test_dir, jobfile) )
try:
p = subprocess.Popen( command.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
except :
print ('from subprocess.Popen( %s )' % command.split() )
raise Exception
#endtry
Unfortunately, here is what I get:
bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF')
Not sure how I can code up that end-of-file statement (I'm guessing the newline chars get in the way here?)
I've done a search on the website but there seem to be no Python examples of this sort...
Here is a minimum working example,the key is that after
<< EOF
the remaining string should not be split. Note thatcommand.split()
is only called once.Verify by checking that the created file
test.txt
shows up in the Downloads directory on the host that you ssh:ed into.Kind regards,
Filip