I have a C++ class used to start and communicate with external processes (somewhat similiar to Qt's QProcess - we can't use Qt as we're working on a small embedded system). It uses pipe and dup2 system calls to establish a link between parent and child process. The problem is - it doesn't work with ssh, scp and other programms expecting a password. I've straced ssh and it seems it opens /dev/tty and somehow uses it to read the password from the command line. Is there any way to input the password from my class or to detect that an appliaction expects input from some other source than stdin (doesn't have to be portable at all)?
It is common practice for ssh programs to read passwords directly from terminals. You need to allocate a pseudo terminal and execute ssh within this pseudo terminal to capture this I/O. The pty man page (section 7, man 7 pty
) is a good introduction.
I'd like to re-state that you do not normally want to give passwords to ssh, there are many better authentication options. Use a public-key authentication for processes that should and may run without user input.
Look for some program that already does it, for instance lftp.
Or strace this Perl one-liner to get an idea of what you should do:
perl -MNet::OpenSSH -e 'Net::OpenSSH->new(localhost, password => foo)'
It mostly reduces to allocating a pseudo-tty pair on the master process, forking and setting the slave part of the pty on the child as the current tty before exec'ing ssh.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With