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 ssh success or not?
How can parent know that child has successfully ssh-ed to remote server, so parent can send something to remote server?
Use the function
waitpidin the parent ( ie inside yourelse if ( pid>0)condition ) to get the status of the childQuoting IBM DeveloperWorks
stat_loc
So if
stat_locis zero, you may assume that SSH terminated normallyEDIT1
If you dont want the parent to be blocked, then you need to set up a signal handler for
SIGCHLDand do the samewaitpidthere. This time as the child has already terminated,waitpidwill return immediatelySomething on these lines
Make
pidglobal so that you can access it fromsigchld_handlertoo. Generallysshreturns0for SUCCESS and255( or some other positive value ) for failure, though I am not cent percent sure on this.EDIT2
From our discussion, I see you want to execute commands as well on the remote server. I suggest you run
sshthisYou can pass these arguments of
sshviaexecl()and as explained in EDIT1, you can check for its return value to verify whether everything went fine. Also, since you are doingsshfrom code, you may not want to enter password manually. Here is how you do password less login