LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2012, 08:38 PM   #1
everbox
LQ Newbie
 
Registered: Mar 2012
Posts: 1

Rep: Reputation: Disabled
Questions about using forkpty to create pseudo terminal to ssh in C


Code:
  pid = forkpty (&pty,0,0,0);
    if (pid == 0) {
        execl ("/usr/bin/ssh", "ssh", hostname, NULL);
        exit (0);
    } else if (pid > 0) {
        ssh_pid = pid;
        ssh_pty = pty;
        if(child_ssh_success()) {
            get_user_input();
            send_user_input_to_child_ssh_and_child_forword_it_to_remote_server();
            get_remote_server_response_from_child();
            display_response_to_stdout();
        }
    }
How can I tell whether child ssh success or not(child_ssh_success())?

How can parent know that child has successfully `ssh-ed` to remote server, so parent can send something to remote server?
 
Old 03-29-2012, 03:39 AM   #2
A.Thyssen
Member
 
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 158

Rep: Reputation: 44
Typically when the ssh does not return with a non-zero exit.

PS; make the exit after the execl() non-zero! or at least report execl failure before exiting.

To know when you have finally connected you need to watch the output from the ssh for errors, login prompts, or shell prompt (success).

The better way is to specify the command you want to run on the ssh line and then just wait from that command to exit.

ASIDE: running background ssh (or telnet) from a shell is just a complex as from a C program. You may like to look at and understand shell co-processing techniques such as
http://www.ict.griffith.edu.au/antho...rocesses.hints
and
http://steve-parker.org/sh/hints.shtml#telnet

The C version would be very similar in style, though without needing named pipes.

Also "ssh" should not require a PTY, though without it you will may not get shell prompts. You may however like to merge STDERR and STDOUT into a single communications pipe (for ssh errors, remote command errors will be on standard output). If you don't then you will need to monitor both stderr and stdout simultaniously (using select() calls).


Did I say this was easy? I look forward to other peoples response to the query.

Last edited by A.Thyssen; 03-29-2012 at 03:41 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create a pseudo terminal jayasekar Programming 3 12-11-2009 08:18 AM
How to create pseudo terminal master and slave trkecea Linux - Kernel 3 11-08-2009 04:27 PM
Questions about (Pseudo)Terminal devices beginner16 Programming 0 04-09-2004 10:03 AM
Pseudo Terminal Problems ailiez Programming 2 02-09-2004 01:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration