0

I'm trying to get user input into a string variable and send it to my Linux server using ssh and C#. I'm using the ssh.net library and I want to pass the variable's value into a text file on the server, but I can't send it together with the echo command. Is there any other way to do it??

Here is the code :

using (var client = new SshClient(host, user, pass))
{
client.Connect();
string message = Console.ReadLine();
var mycommand = client.RunCommand("echo message >> file.txt");
client.Disconnect();
}
0

1 Answer 1

1

You are currently passing "message" as a string. All you need to do is append the contents of "message" variable instead,

using (var client = new SshClient(host, user, pass))
{
client.Connect();
string message = Console.ReadLine();
var mycommand = client.RunCommand("echo " + message + " >> file.txt");
client.Disconnect();
}
Sign up to request clarification or add additional context in comments.

2 Comments

You need to escape the string... Use: "echo \"" + message + "\" >> file.txt"
interpolation make everything cleaner: $"echo \"{message}\" >> file.txt"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.