【问题标题】:How to invoke ssh exit from the C program under Linux Platform?Linux平台下如何从C程序调用ssh退出?
【发布时间】: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。

标签: c linux ssh


【解决方案1】:

在子进程中,标准输出不会重定向到您的管道,您需要使用例如dup2 像这样:

dup2(fd[1], STDOUT_FILENO);

在致电system之前。

并且不要使用system 来执行程序,而是使用exec 系列函数。

所以子进程应该是这样的:

if( pid == 0 )
{
    // Make standard output use our pile
    dup2(fd[1], STDOUT_FILENO);

    // Don't need the pipe descriptors anymore
    close(fd[0]);
    close(fd[1]);

    // Execute the program
    execlp("ssh", "ssh", "-tt", destIp, NULL);
}

此外,在父进程中,您需要在完成后为子进程wait


如果您不想打扰管道和进程以及等待等,只需改用popen,它会为您处理所有事情,并为您提供一个不错的FILE *,您可以使用。

【讨论】:

  • 我将如何退出 ssh ?
【解决方案2】:

我想要一个丑陋的方式来做到这一点。但是,在一个大型程序中,我们需要维护一个打开了哪些 SSH 会话的状态表。这是关闭第一个 SSH 会话的程序。

int exit_ssh()
{
    FILE *in;
    char buff[512];
    char sshCtrlword[512];
    printf("ssh Exit Invoked\r\n");
    memset(buff,'\0',sizeof(buff));

    if(!(in = popen("ps -A x |grep sshd |grep root |grep -v grep", "r")))
        if(!(in = popen(gsshCurrentSession,"r")))
        {
            printf("Error in Popen\r\n");
            return -1;
        }
    while(fgets(buff, sizeof(buff), in)!=NULL)
        while(fgets(buff, sizeof(buff), in) != NULL)
        {
            //printf("%s\r\n", buff);
        }
    pclose(in);

    if( buff[0] != '\0')
    {
        printf("%s\r\n", buff);
        /** we have got something **/
        /** prepare control word **/
        sprintf(sshCtrlword,"kill -SIGTERM %s",buff);
        printf("%s\r\n", sshCtrlword);

        if(!(in = popen(sshCtrlword,"r")))
        {
            printf("Error in Popen() ctrl word..\r\n");
            return -1;
        }
        pclose(in);
    }

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 2015-04-29
    • 2021-03-14
    • 2019-01-21
    相关资源
    最近更新 更多