【问题标题】:How to run makefile with libssh?如何使用 libssh 运行 makefile?
【发布时间】:2013-01-26 15:11:06
【问题描述】:

我对 libssh (libssh.org) 有疑问。我需要在远程服务器上运行一个 makefile。我使用命令“channel_request_exec”:

int SSHExecCmd (void(* MessSender)(char* CurMessage, bool IsError, CWnd* MainWnd),ssh_session session,  CString & ShellEcho, char * cmd, CWnd* MainWnd)
{
    ssh_channel channel;
    int rc;
    channel = ssh_channel_new(session);
    if (channel == NULL) return SSH_ERROR;
    rc = ssh_channel_open_session(channel);
    if (rc != SSH_OK)
    {
        ssh_channel_free(channel);
        return rc;
    }
    rc = ssh_channel_request_exec(channel, cmd);
    if (rc != SSH_OK)
    {
        ssh_channel_close(channel);
        ssh_channel_free(channel);
        return rc;
    }
    char buffer[256];
    unsigned int nbytes;
    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    while (nbytes > 0)
    {
        if (fwrite(buffer, 1, nbytes, stdout) != nbytes)
        {
            ssh_channel_close(channel);
            ssh_channel_free(channel);
            return SSH_ERROR;
        }
        nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    }
    if (nbytes < 0)
    {
        ssh_channel_close(channel);
        ssh_channel_free(channel);
        return SSH_ERROR;
    }
    return SSH_OK;
}

Makefile 位于根目录:

all: mpi_cuda.o pattern2d.o
        mpicc mpi_cuda.o pattern2d.o -o mpi_cuda -lrt -lpthread -L/opt/cuda/lib64 -lcudart -lm

mpi_cuda.o: mpi_cuda.c
        mpicc -g -std=c99 -D_GNU_SOURCE -I/opt/cuda/include -c $< -o $@

pattern2d.o: pattern2d.cu
        nvcc -g -c $< -o $@

我发送命令“make”并接收回显:

mpicc -g -std=c99 -D_GNU_SOURCE -I/opt/cuda/include -c mpi_cuda.c -o mpi_cuda.oda

但没有任何反应(不执行编译)。

如果我确实使用腻子:一切正常。回声:

make
mpicc -g -std=c99 -D_GNU_SOURCE -I/opt/cuda/include -c mpi_cuda.c -o mpi_cuda.o
mpi_cuda.c: В функции ‘main’:
mpi_cuda.c:148: предупреждение: недостаточно аргументов для указанного формата
nvcc -g -c pattern2d.cu -o pattern2d.o
mpicc mpi_cuda.o pattern2d.o -o mpi_cuda -lrt -lpthread -L/opt/cuda/lib64 -lcudart -lm

我该如何解决这个问题?

【问题讨论】:

    标签: c++ makefile libssh


    【解决方案1】:

    不熟悉 libssh,但可能会出错,因为环境设置不同,因此通过 shell 显式运行 make 可能会有所帮助。

    尝试将命令 (make?) 更改为

    bash -c make
    

    如果不行,试试

    bash -c "export > env.txt ; make > make_out.txt 2> make_err.txt"
    

    然后检查这些文件是否出现,以及它们包含的内容,这应该会给出很好的提示。

    如果您有一个工作案例和一个不工作案例,则从这两个案例中获取这些文件,并比较它们(例如与diff -u)。

    并将bash 更改为您使用的任何外壳(在这种情况下,检查-c 是否是正确的开关以提供命令字符串,如果export 是正确的命令以显示环境),如果您不使用重击。


    基于以下 cmets: env.txt 中的差异可能是,因为某些环境变量只为交互式 shell 设置。例如,在我的 Ubuntu 框中,.bashrc 的开头有这样的行:

    # If not running interactively, don't do anything
    [ -z "$PS1" ] && return
    

    现在,如果在 .bashrc 中该行之后设置了任何所需的环境变量,并且您的 ssh 连接是非交互式的(没有伪 tty),则不会设置这些变量。

    如果是这种情况,请将这些环境变量集移至~/.profile,或在上述测试之前移至~/.bashrc。也做man bash,并阅读有关初始化文件的内容(如~/.bashrc)。

    其他解决方案是使 ssh-session 具有交互性,我相信在此页面上为 libssh 记录了这一点:http://api.libssh.org/master/libssh_tutor_shell.html。

    【讨论】:

    • 我在两台服务器上测试了日志记录选项 (bash -c "make > make_out.txt 2> make_err.txt")。首先 make_err.txt: make: mpicc: Command not found make: *** [all] Error 127 Second: make: nvcc: Command not found make: *** [pattern2d.o] Error 127 通过反复试验我收到了仅在一台服务器上工作的错误代码。使用异步读取(ssh_channel_read_nonblocking)。但是为什么在这种情况下可以使用 mpicc 呢?很奇怪……
    • @snk 更新了将环境转储到 env.txt 的答案,以及关于比较工作和非工作输出(尤其是 env.txt)的说明。
    • 抱歉耽搁了。当您使用 libssh 连接时,env.txt 包含 12 个变量。使用 Putty 34。问题在这里,但为什么不清楚。
    • 你读过这个页面吗:api.libssh.org/master/libssh_tutor_shell.html?最重要的是,阅读有关交互式与非交互式 shell 的内容。 (注意:我没有阅读该页面,我只是通过谷歌搜索找到它;)。
    • 添加了要回答的内容...(另外,由于您是 SO 的新手:您应该对所有您认为有用的答案进行投票,然后一旦您对答案感到满意,请记住接受你最喜欢或认为正确的那个)。
    【解决方案2】:

    我建议打开一个非交互式 shell 并在那里进行。见

    http://api.libssh.org/master/libssh_tutor_shell.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-29
      • 2012-07-09
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2022-10-05
      • 2022-11-20
      相关资源
      最近更新 更多