Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: Launch distant bash script

$
0
0
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,

Viewing all articles
Browse latest Browse all 1729

Latest Images

Trending Articles



Latest Images