I want to programmatically control the SSH or telnet to login from a running process. I tried using redirection through pipes but ssh returned an error saying something like stdin is not a terminal. Now, my search has finally led to me a solution: through the use of pseudoterminals. So I made a test program (listed below).
Supposedly I can write to stdin by writing to the file desc of the master pty. But it seems the co-process (ssh) is not reading what I wrote from the slave pty. I don't know what's wrong?
Here is the test program:
if (pid = forkpty(&fds,sSlave, NULL, NULL ) == 0)
execl("/usr/bin/ssh","ssh", "root@localhost", NULL);
// Simulate a user typing his username and password
// to the terminal
write(fds, "username", strlen("username"));
write(fds, "password", strlen("password"));