2013-01-20 26 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