Search code examples
c#.netsshssh.netavaya

C# send Ctrl+Y over SSH.NET


I am currently using SSH.net and trying to connect to an Avaya switch.

My code looks like this:

using (var client = new SshClient(ip, username, password))
{
    client.Connect();
    //Sending CTRL+Y 
    SshCommand x = client.RunCommand("enable");
    client.Disconnect();
}

I haven't found a solution yet.

Is there any workaround or command I could use to send my CTRL+Y?


Solution

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