【发布时间】:2016-05-30 06:13:54
【问题描述】:
我现有的应用程序有一个定制的 CLI - 命令行界面。我正在尝试使用自定义 CLI 将 ssh 从现有应用程序调用到运行相同应用程序的远程 PC。我无法使用 lib ssh 创建会话,但我想使用现有的 Linux SSH 应用程序。 这是代码,我用来从驻留在一台 PC 中的一个应用程序调用 ssh 到另一台 PC。我的问题是如何退出 SSH。我看到调用 exit 没有任何影响。我应该怎么做?这是我的 SSH 示例程序。
INT4 do_ssh(tCliHandle CliHandle, CHR1 *destIp)
{
FILE *writePipe = NULL;
char readbuff[1024];
char cmd[1024];
pid_t pid;
int fd[2];
int childInputFD;
int status;
memset(cmd,'\0',sizeof(cmd));
sprintf(cmd,"/usr/bin/ssh -tt %s",destIp);
/** Enable For debugging **/
//printf("cmd = %s\r\n",cmd);
/** create a pipe this will be shared on fork() **/
pipe(fd);
if((pid = fork()) == -1)
{
perror("fork");
return -1;
}
if( pid == 0 )
{
gchildPid = getpid();
system(cmd);
}
else
{
/** parent process -APP process this is **/
while( read(fd[0], readbuff, sizeof(readbuff)) != 0 )
{
CliPrintf(CliHandle,"%s", readbuff);
printf("%s", readbuff);
}
close(fd[0]);
close(fd[1]);
}
return 0;
}
结果 - 我可以看到 ssh 被调用 - 我可以输入密码并可以在远程 PC 应用程序上执行 SSH。但是,我不知道如何退出 SSH 会话。我应该怎么做才能退出 SSH 会话?
【问题讨论】:
-
即使出现错误,您的循环
while (read(...) != 0)也会继续尝试读取。如果你得到一个错误读取,你将有一个无限循环。 -
ssh成功后,如何退出ssh会话。我无法退出,因为 ssh 打开了另一个自定义应用程序提示。所以,我不能从自定义提示中调用 exit。