I need in python execute this command and enter password from keyboard, this is works:
import os
cmd = "cat /home/user1/.ssh/id_rsa.pub | ssh user2@host.net \'cat >> .ssh/authorized_keys\' > /dev/null 2>&1"
os.system(cmd)
As you can see I want append public key to remote host via ssh.
See here: equivalent-of-ftp-put-and-append-in-scp and here: copy-and-append-files-to-a-remote-machine-cat-error
Of course I want it do it without user input I've try pexpect and I think command is to weird for it:
import pexpect
child = pexpect.spawn(command=cmd, timeout=10, logfile=open('debug.txt', 'a+'))
matched = child.expect(['Password:', pexpect.EOF, pexpect.TIMEOUT])
if matched == 0:
child.sendline(passwd)
in debug.txt:
ssh-rsa AAAA..........vcxv233x5v3543sfsfvsv user1@host1
/bin/cat: |: No such file or directory
/bin/cat: ssh: No such file or directory
/bin/cat: user2@host.net: No such file or directory
/bin/cat: cat >> .ssh/authorized_keys: No such file or directory
/bin/cat: >: No such file or directory
/bin/cat: 2>&1: No such file or directory
I see two solution:
- fix command for pexpect, that it recognize whole string as one command or,
- inject/write passwd to stdin as fake user, but how!?!?