Question

On OSX Terminal, I can connect to a server with SSH because terminal offers password input session.

$ ssh user@server.com
user@server.com's password: 

I wanted to make my own terminal implementation, so I

  1. created pseudo terminal.
  2. fork
  3. redirected pty slave into stdin on child process.
  4. exec bash.

And then, pushes string ssh user@server.com into pty master on parent process. But by program showed me this.

$ ssh user@server.com
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).

instead of password session.

What's my problem, and how can I fix it?

P.S. I know I can use expect utility. This is just a trial to implement my own pseudo shell application.

Update for later reference

The key to solution was using of forkpty() function instead of posix_openpt() function. Former one handles all hard annoying works, and gives me bi-directional master file number. I can read from and write to the file number. And the created pty device works perfectly. SSH login session works well. In contrast latter one still needs a lot of works which I never can figure out how.

I had to care about forkpty last two parameters. I thought them was output, but actually it was input. That was the source of all the problems, and after setting them to all NULL, it's working well now.

Was it helpful?

Solution

There is already a program which does what you want: sshpass

http://sshpass.sourceforge.net/

Since it is open source, you can examine its source to find out how it works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top