2013-01-20 48 views
2

我有一个libssh(libssh.org)的问题。我需要在远程服务器上运行makefile。我用命令 “channel_request_exec” 做到这一点:如何用libssh运行makefile?

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 [email protected] 

pattern2d.o: pattern2d.cu 
     nvcc -g -c $< -o [email protected] 

我发送一个命令 “制造” 和回波接收:

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

,但没有发生(编译不执行)。

如果我用油灰使:一切工作。 Echo:

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 

我该如何解决这个问题?

回答

1

不熟悉libssh,但错误可能是因为环境被设置不同,所以运行make经过加壳明确可能会有帮助。

尝试改变命令(make?)到

bash -c make 

如果还是不行,请尝试

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

然后检查这些文件的出现,以及它们包含的内容,应给予良好提示。

如果您有工作案例和非工作案例,那么从这两种情况下获取这些文件并对其进行比较(例如,使用diff -u)。

,并更改bash你使用任何壳(在这种情况下,检查是否-c是给命令字符串右边的开关,如果export是正确的命令来显示环境),如果你不使用bash。


基于以下评论:env.txt中的差异可能是,因为一些环境变量只为交互式shell设置。举例来说,在我的Ubuntu框中的.bashrc开始有这样的台词:

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

现在是否有这些需要environemnt变量的.bashrc中设置该行后,你的SSH连接非交互式(没有伪tty),这些不会被设置。

如果是这种情况,请在进行上述测试之前将这些env变量集合移动到~/.profile~/.bashrc。也做man bash,并阅读有关初始化文件的东西(如~/.bashrc)。

其他的解决办法是让SSH会话互动,我相信在这个页面被记录为libssh:http://api.libssh.org/master/libssh_tutor_shell.html

+0

我在两台服务器上测试选项与日志记录(bash的-c “做法> make_out.txt 2> make_err.txt”)。 首先make_err.txt: 化妆:找不到命令 化妆:mpicc *** [全部]错误127 二: 化妆:NVCC:命令未找到 化妆:*** [pattern2d.o ]错误127 通过试验和错误我收到的错误代码只能在一台服务器上工作。使用异步读取(ssh_channel_read_nonblocking)。 但是为什么在这种情况下可以使用mpicc?这很奇怪...... – snk

+0

@snk更新了将环境转储到env.txt的答案,以及有关比较工作和非工作输出(尤其是env.txt)的注释。 – hyde

+0

抱歉,延迟。当你连接libssh时,env.txt由12个变量组成。用腻子34.这里的问题,但为什么不明确。 – snk