Launch distant bash script

May 30, 2013 at 10:23 AM

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)) {

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

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

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?

May 30, 2013 at 7:54 PM
Edited May 30, 2013 at 8: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.