nutsman nutsman - 1 month ago 20
Linux Question

calling write() over ssh

I wrote the following code on a host called

node00
and save as
main.c
.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main(void){
int fd;
const char str[300]="hello world\n";
fd = open("NOTE", O_RDWR | O_CREAT);

if(fd != 0){

write(fd, str, sizeof(str));
close(fd);
puts("succ");
}
return 0;
}


Then, I compiled it to
main
.
After that, I run
ssh node00 TEST/main
on another machine, but the file
NOTE
does not exist in the
node00
, although I run
./main
on the
node00
, finding the file
NOTE
containing
hello world
.

I would ask you to give me an idea to run the program as expected.

Thank you guys for all comments and answers, in advance.

Answer

Running your command through SSH like that, the working directory will be your home on node00 so the NOTE file will be created in the parent directory of the TEST directory.

If you want NOTE to be created alongside the main file in the TEST directory, you can try something like

ssh node00 "cd TEST && ./main"