Reputation: 564
how to perform
echo xyz | ssh [host]
(send xyz to host) with python?
I have tried pexpect
pexpect.spawn('echo xyz | ssh [host]')
but it's performing
echo 'xyz | ssh [host]'
maybe other package will be better?
Upvotes: 0
Views: 216
Reputation: 155436
You don't need pexpect
to simulate a simple shell pipeline. The simplest way to emulate the pipeline is the os.system
function:
os.system("echo xyz | ssh [host]")
A more Pythonic approach is to use the subprocess
module:
p = subprocess.Popen(["ssh", "host"],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write("xyz\n")
output = p.communicate()[0]
Upvotes: 0
Reputation: 9724
http://pexpect.sourceforge.net/pexpect.html#spawn
Gives an example of running a command with a pipe :
shell_cmd = 'ls -l | grep LOG > log_list.txt'
child = pexpect.spawn('/bin/bash', ['-c', shell_cmd])
child.expect(pexpect.EOF)
Previous incorrect attempt deleted to make sure no-one is confused by it.
Upvotes: 2