Launch distant bash script

May 30, 2013 at 11:23 AM
Hi!

I'm using SSH.NET to launch bash scripts (stored in my database) on distant computers.
using (SshClient client = new SshClient(execution.Host, 22, execution.User, key)) {
    client.Connect();

    SshCommand cmd = client.CreateCommand(execution.Script);
    cmd.Execute();

    execution.Result = cmd.Result;
    execution.ExitCode = cmd.ExitStatus;

    client.Disconnect();
}
execution.Script contains a script like, for instance:
# Get time as a UNIX timestamp (seconds elapsed since Jan 1, 1970 0:00 UTC)
findcpu() {
    grep 'model name' /proc/cpuinfo  | uniq | awk -F':' '{ print $2}'
}

findkernelversion() {
    uname -mrs
}

totalmem() {
    grep -i 'memtotal' /proc/meminfo | awk -F':' '{ print $2}'
}

echo "CPU Type : $(findcpu)"
echo "Kernel version : $(findkernelversion)"
echo "Total memory : $(totalmem)"
When i try to run this script directly on the distant computer, everything is fine. But with SSH.NET, I have an exit code of 1.

It works with smaller script, like for instance
T="$(date +%s)"

# Do some work here
sleep 2

T="$(($(date +%s)-T))"
So I'm a bit lost on this.. I just want to execute a script like a local user on the computer and to retrieve the output and the exit code...

Do you have any ideas?

Regards,
May 30, 2013 at 8:54 PM
Edited May 30, 2013 at 9:28 PM
function findcpu() { grep 'model name' /proc/cpuinfo  | uniq | awk -F':' '{ print $2}' ; }; function findkernelversion() { uname -mrs ;}; function totalmem() { grep -i 'memtotal' /proc/meminfo | awk -F':' '{ print $2}'; }; echo "CPU Type : $(findcpu)"; echo "Kernel version : $(findkernelversion)";echo "Total memory : $(totalmem)"
How's that? Works in Bash. I tested this using SshCommand as well and it seems to work fine to a server with a default shell of bash.