I'm attempting to use a subprocess to interact with some files through sftp. I can get it to connect and exec one command, but then the process dies. How can I maintain control over this process and keep moving around in sftp?
import os
import subprocess
serverFiles = 'sftp.servername.com'
userFiles = 'myusername'
keyfileFiles = 'C:\\key\\file\\path'
with subprocess.Popen(['sftp','-Q','-i',keyfileFiles,userFiles+'@'+serverFiles], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT) as proc:
(out, err) = proc.communicate(input='ls -l'.encode())
print(out.decode())
#process dies. cannot proc.stdin.write, cannot proc.communicate
My output shows me the welcome banner, then the ls from the top level folder, then the process is ended and any following commands error out with proc
being closed.