2

I have a problem with libssh (libssh.org). I need to run a makefile on the remote server. I do it with a command "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 is located in root:

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 $@

I send a command "make" and receive echo:

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

but nothing happens (compilation is not performed).

If I do make with a putty: everything work. 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

How do I solve this?

snk
  • 175
  • 3
  • 10

2 Answers2

1

Not familiar with libssh, but error could be, because environment gets set up differently, so running make through shell explicitly could help.

Try changing the command (make?) to

bash -c make

If that does not work, try

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

Then check if those file appeared, and what they contain, that should give good hints.

If you have a working and a non-working case, then get these files from both cases, and compare them (eg. with diff -u).

And change bash to whatever shell you use (and in that case check if -c is right switch to give command string, and if export is right command to show environment), if you're not using bash.


Based on comments below: Difference in env.txt could be, because some of the environment variables only get set for interactive shells. For example, in my Ubuntu box, start of .bashrc has lines like this:

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

Now if any of those needed environemnt variables are set in .bashrc after that line, and your ssh connection is non-interactive (without pseudo-tty), these don't get set.

If this is the case, move these env variable sets to ~/.profile, or in ~/.bashrc before such test as above. Also do man bash, and read stuff about initialization files (like that ~/.bashrc).

Other solution would be to make the ssh-session interactive, which I believe is documented at this page for libssh: http://api.libssh.org/master/libssh_tutor_shell.html .

hyde
  • 60,639
  • 21
  • 115
  • 176
  • I tested option with logging (bash -c "make > make_out.txt 2> make_err.txt") on two servers. First make_err.txt: make: mpicc: Command not found make: *** [all] Error 127 Second: make: nvcc: Command not found make: *** [pattern2d.o] Error 127 By trial and error I received the bad code working only at one server. Asynchronous reading is used (ssh_channel_read_nonblocking). But why mpicc is available in this case? it's very strange... – snk Jan 20 '13 at 11:48
  • @snk Updated the answer to also dump environment to env.txt, as well as a note about comparing working and non-working outputs (especially env.txt). – hyde Jan 20 '13 at 14:37
  • Sorry for the delay. When you connect with libssh env.txt consist of 12 variables. With Putty 34. The problem here, but why is not clear. – snk Jan 26 '13 at 03:23
  • Have youread this page: http://api.libssh.org/master/libssh_tutor_shell.html ? Most importantly, read stuff about interactive vs. non-interactive shells. (Note: I did not read that page, I just did one google search to find it ;). – hyde Jan 26 '13 at 06:57
  • Added stuff to answer... (Also, since you're new at SO: you should up-vote all answers you think are useful, and then once you're satisfied with answers, remember to accept the one you like best or think is correct). – hyde Jan 26 '13 at 07:12
  • I solved it via interactive shell. Thank's! I can not vote because of a low reputation. – snk Jan 26 '13 at 12:01
1

I would suggest to open a non-interactive shell and do it there. See

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

asn
  • 798
  • 9
  • 17