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 waitpid
in the parent ( ie inside your else if ( pid>0)
condition ) to get the status of the child
#include <sys/wait.h>
pid_t waitpid(pid_t pid, int *stat_loc, int options);
Quoting IBM DeveloperWorks
stat_loc
A pointer to an integer where the wait function will return the status of the child process. If the waitpid function returns because a process has exited, the return value is equal to the pid of the exiting process. For this, if the value of stat_loc is not NULL, information is stored in the location pointed to by stat_loc. If status is returned from a terminated child process that returned a value of 0, the value stored at the location pointed to by stat_loc is 0. If the return is greater than 0, you can evaluate this information using the following macros: WEXITSTATUS WIFEXITED WIFSIGNALED WTERMSIG.
So if stat_loc
is zero, you may assume that SSH terminated normally
EDIT1
If you dont want the parent to be blocked, then you need to set up a signal handler for SIGCHLD
and do the same waitpid
there. This time as the child has already terminated, waitpid
will return immediately
Something on these lines
pid_t pid;
int main ()
{
struct sigaction action;
memset (&action, 0, sizeof(action));
action.sa_handler = sigchld_handler;
if (sigaction(SIGCHLD, &action, 0))
{
perror ("sigaction");
return 1;
}
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;
}
}
/* SIGCHLD handler. */
static void sigchld_handler (int sig)
{
int chld_state;
while (waitpid(pid,&child_state,options) > 0)
{
if (WIFEXITED(chld_state))
{
printf("Child exited with RC=%d\n",WEXITSTATUS(chld_state));
}
}
}
Make pid
global so that you can access it from sigchld_handler
too. Generally ssh
returns 0
for SUCCESS and 255
( 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 ssh
this
ssh root@remoteserver.com 'ls -l'
You can pass these arguments of ssh
via execl()
and as explained in EDIT1 , you can check for its return value to verify whether everything went fine. Also, since you are doing ssh
from code, you may not want to enter password manually. Here is how you do password less login
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.