Ok, here is a tricky question:
First, I have a very small C application that runs in an Unix machine. This application gets two arguments and tries to execute an .sh file in a Linux (Ubuntu) machine, like this:
./myapp luke leia
Ok, now the C code executes more or less this:
char temp[512];
sprintf(temp, "ssh user@server \"sh \\$HOME/save.sh %s %s\"", argv[1], argv[2]);
system((char *)temp);
As you can see, it opens a connection to my Linux server and executes a batch script in the home folder of my ‘user’. This is how it looks like when I try directly in my terminal:
ssh user@server "sh \$HOME/save.sh luke leia"
In my Unix side, I am asked for a password for the SSH, and it works just fine. I get all the echoes from the save.sh file in my Unix machine and it works well, except if I need a sudo permission. So I tried this in my server:
-
Using
sudo visudoin the server, below the line that says%sudo ALL=(ALL) ALLI put:%user ALL=(ALL) NOPASSWD: /home/user/save.sh -
My save.sh looks like this:
#! /bin/bash sudo echo "[save action] args: ${1} , ${2}" >> /somewhere/save.log; echo "${1} and ${2} are brother and sister.";
- My folder “somewhere” belongs to root, so normally my ‘user’ can’t save it. So I changed the code in my application to handle this command instead:
ssh user@server "sudo sh \$HOME/save.sh luke leia"
Now I get this error:
sudo: no tty present and no askpass program specified
Which is the best way to trigger this script using sudo and not being prompted with root password? I tried using -t, but is asks for root password. Any thoughts?
Solution that comes to my mind is to provide the root passowrd of “server” machine as additional parameter (argv[3]) in your C program:
-S for sudo means that password will be provided via stdin, so no user interaction should be needed. It’s theoretical solution (I do not have time to check it – sorry) check it for your self & please tell as whether it works.