I am trying to execute a bash shell script from Windows on a remote Linux machine.
I am using c# and the SSH.Net library.
The scripts live on the windows box and cannot be installed on the Linux machine. I read in the script using 'File.ReadAllText(...)' which loads in the script as string. Using SSH.Net I then execute the script on Linux:
SshCommand cmd;
using (var client = new SshClient(ConnectionInfo))
{
client.Connect();
cmd = client.CreateCommand(string.Format("sh -x -s < {0}", script));
cmd.Execute();
client.Disconnect();
}
return cmd.ExitStatus;
This works when the script doesn't have any parameters. But if I need to pass in some arguments the following executes the script but the params are missing:
cmd = client.CreateCommand(string.Format("sh -x -s p={0} < {1}", parameterString, script));
The sample script is:
#!/bin/bash
# check-user-is-not-root.sh
echo "Currently running $0 script"
echo "This Parameter Count is [$#]"
echo "All Parameters [$@]"
The output is:
Currently running bash script
This Parameter Count is [0]
All Parameters []
Update
For now I am using curl (like in the approved answer here:).
cmd = client.CreateCommand(string.Format("curl http://10.10.11.11/{0} | bash -s {1}", scriptName, args))
But I still think there must be a way to read in a bash script with arguments and run it across ssh on a remote Linux box.
Any help would be greatly appreciated.