Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pipe the password to ssh in C++?

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)?

like image 306
Zbigh1 Avatar asked Nov 07 '11 09:11

Zbigh1


2 Answers

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.

like image 81
thiton Avatar answered Sep 22 '22 13:09

thiton


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.

like image 27
salva Avatar answered Sep 23 '22 13:09

salva