C# Send a Simple Ssh Command

C# send a simple SSH command

I used SSH.Net in a project a while ago and was very happy with it. It also comes with a good documentation with lots of samples on how to use it.

The original package website can be still found here, including the documentation (which currently isn't available on GitHub).

For your case the code would be something like this.

using (var client = new SshClient("hostnameOrIp", "username", "password"))
{
client.Connect();
client.RunCommand("etc/init.d/networking restart");
client.Disconnect();
}

C# Simple SSH Connection And Send Command

Use:

SshCommand x = cSSH.RunCommand(textbox3.Text);

Without quotes.

C# send Ctrl+Y over SSH.NET

SSH.NET does not support sending an input to commands executed with SshClient.RunCommand ("exec" channel).

You have to use a "shell" channel (SshClient.CreateShell or SshClient.CreateShellStream). This is normally not recommended for a command automation. Even more so with SSH.NET, which does not even allow your to turn off a pseudo terminal for the "shell" channel. This brings lots of nasty side effects, particularly with "smart" shells on Linux. But with devices likes switches, it might be bearable.

Also particularly sending control sequences like Ctrl+Y is more complicated with the pseudo terminal. But again, let's hope that the "shell" of the switch is dumb enough to simply accept a pure ASCII cide for Ctrl+Y, what is 25 = x19.

ShellStream shellStream = client.CreateShellStream(string.Empty, 0, 0, 0, 0, 0);
shellStream.Write("enable\n");
shellStream.Write("\u19");

while (true)
{
string s = shellStream.Read();
Console.Write(s);
}

C# - send variables as linux commands over ssh

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();
}

C# SSH Text, number Send?

My understanding of your flow is you chmod and start a script, pass some inputs to it and wait for it to finish. If I get your problem statement correctly, you might be better off using ShellStream. This way your interactivity becomes an exercise or sening a command and Expecting a certain response:

    /*
#i used the following script on the other side to simulate activity:

#!/bin/bash

echo "Input time to sleep:"
read timeToSleep
sleep $timeToSleep
echo "We are done"
*/
using (var client = new SshClient(host, user, pass))
{
client.Connect();
var shell = client.CreateShellStream("bash", 80, 50, 1024, 1024, 1024); // I believe shell name is critical to get your control character sequences parsing right

shell.Expect(new ExpectAction("#", (_) => // we wait for shell prompt here. if your shell is different you might need to tweak the regex
{
shell.WriteLine("chmod +x xxx.sh && ./xxx.sh");
}));
shell.Expect(new ExpectAction("Input time to sleep:", (_) => {
shell.WriteLine("5");
}));
shell.Expect(new ExpectAction("#", scriptOutput => {
Console.WriteLine(scriptOutput);
}));

client.Disconnect();
}

As you havent shared the actual script that you run, i can't be more specific. Hopefully that sets you on track to flesh the details out.

How to run commands on SSH server in C#?

You could try https://sshnet.codeplex.com/.
With this you wouldn't need putty or a window at all.
You can get the responses too.
It would look sth. like this.

SshClient sshclient = new SshClient("172.0.0.1", userName, password);    
sshclient.Connect();
SshCommand sc= sshclient .CreateCommand("Your Commands here");
sc.Execute();
string answer = sc.Result;

Edit: Another approach would be to use a shellstream.

Create a ShellStream once like:

ShellStream stream = sshclient.CreateShellStream("customCommand", 80, 24, 800, 600, 1024);

Then you can use a command like this:

  public StringBuilder sendCommand(string customCMD)
{
StringBuilder answer;

var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
writer.AutoFlush = true;
WriteStream(customCMD, writer, stream);
answer = ReadStream(reader);
return answer;
}

private void WriteStream(string cmd, StreamWriter writer, ShellStream stream)
{
writer.WriteLine(cmd);
while (stream.Length == 0)
{
Thread.Sleep(500);
}
}

private StringBuilder ReadStream(StreamReader reader)
{
StringBuilder result = new StringBuilder();

string line;
while ((line = reader.ReadLine()) != null)
{
result.AppendLine(line);
}
return result;
}

C# SSH Connection

Use output.Result instead of output.ToString().

using System;
using Renci.SshNet;

namespace SSHconsole
{
class MainClass
{
public static void Main (string[] args)
{
//Connection information
string user = "sshuser";
string pass = "********";
string host = "127.0.0.1";

//Set up the SSH connection
using (var client = new SshClient(host, user, pass))
{
//Start the connection
client.Connect();
var output = client.RunCommand("echo test");
client.Disconnect();
Console.WriteLine(output.Result);
}
}
}
}


Related Topics



Leave a reply



Submit