Bash
При использовании bash непосредственно на ssh-терминале работает следующая команда:
Код: Выделить всё
/bin/bash -c 'var="Hello World!"; echo "$var"'
Привет, мир!
C#
При использовании следующего примера кода возникает ошибка:
Код: Выделить всё
using System.Diagnostics;
string command = "-c 'var=\"Hello World!\"; echo \"$var\"'";
Console.WriteLine("Command is: " + command);
string response = SendCommand(command);
Console.WriteLine("Response: " + response);
string SendCommand(string command)
{
string processResponse = string.Empty;
try
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = command,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
};
Process shellProcess = new Process()
{
StartInfo = startInfo,
};
shellProcess.Start();
StreamReader outputStreamReader = shellProcess.StandardOutput;
StreamReader errorStreamReader = shellProcess.StandardError;
string output = outputStreamReader.ReadToEnd();
string error = errorStreamReader.ReadToEnd();
if (output != string.Empty)
{
Console.WriteLine("Process Output Message: {output} ", output);
processResponse = output;
}
shellProcess.WaitForExit(100);
if (shellProcess.ExitCode != 0)
{
if (error != string.Empty)
{
processResponse = error;
Console.WriteLine("Process Error Message: {error} for command {cmd}.", error, command);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error to Send Process Command: " + ex.Message);
}
return processResponse;
}
Код: Выделить всё
Command is: -c 'var="Hello World!"; echo "$var"'
Error to Send Process Command: Input string was not in a correct format.
Response: echo: -c: line 1: unexpected EOF while looking for matching `''
echo: -c: line 2: syntax error: unexpected end of file
Команда вывода аналогична той, которая используется непосредственно на терминале ssh.
Как отправить команду, используя C# в качестве ssh-терминала?
Подробнее здесь: https://stackoverflow.com/questions/792 ... sh-using-c