SSH welcome message / MoTD

+1 vote
asked Sep 9, 2013 by Noatak (360 points)
edited Sep 10, 2013

When connecting to SSH (for example with Putty) there is often a welcome message or a message of the day. Is there any way to use Rebex SSH to read this message?

To clarify, I know that on *nix systems this message is stored in /etc/motd, but I'd like to get the message on any kind of system, without running commands. What I'm looking for is something like this:

Ssh ssh = new Ssh(); ssh.Connect("1.2.3.4", 22); ssh.Login("user", "password"); Console.WriteLine(ssh.Session.WelcomeMessage);

2 Answers

+1 vote
answered Sep 10, 2013 by Tomas Knopp (58,890 points)
edited Sep 10, 2013
 
Best answer

To be able to read the welcome message from the SSH shell, you have two possibilities:

1) if you know the prompt of the server, you can use Ssh object's StartShell method, supply the expected Prompt and then you will be able to read the welcome message like this:

Ssh ssh = new Ssh();
ssh.Connect("server", 22);
ssh.Login("username", "password");

Shell shell = ssh.StartShell(ShellMode.Prompt);
shell.Prompt = "expected_prompt ";
string welcome = shell.ReadAll();
Console.WriteLine(welcome);

2) if you do not know the prompt of the server in advance you can take advantage of the StartVirtualTerminal method and the VirtualTerminal class. You would have to Process the data comming from the SSH server for a constant amount of time (depending on your possibilities and requirements) as in the following code:

Ssh ssh = new Ssh();
ssh.Connect("server", 22);
ssh.Login("username", "password");

var terminal = ssh.StartVirtualTerminal();
var welcome = GetWelcomeMessage(terminal, 3 * 1000);
Console.WriteLine(welcome);

private string GetWelcomeMessage(VirtualTerminal terminal, int milliseconds)
{
    int start = Environment.TickCount;
    string data = string.Empty;
    while (terminal.Process(milliseconds) == TerminalState.DataReceived)
    {
        data += terminal.ReceivedData;
        milliseconds -= Environment.TickCount - start;
        if (milliseconds <= 0)
            break;
    }
    // remove last line which is only the server's prompt
    int idx = data.LastIndexOf('\n');
    if (idx > 0)
        data = data.Substring(0, idx);
    return data;
}
commented Sep 10, 2013 by Noatak (360 points)
edited Sep 10, 2013

The second method works perfectly, thank you!

0 votes
answered Sep 9, 2013 by Tomas Knopp (58,890 points)
edited Sep 10, 2013

You can read the SSH server welcome message with the ServerIdentification property like this:

Ssh ssh = new Ssh();
ssh.Connect("sshServer", 22);
ssh.Login("username", "password");
Console.WriteLine(ssh.Session.ServerIdentification);
commented Sep 10, 2013 by Noatak (360 points)
edited Sep 10, 2013

This returns the ID of the SSH server, not the motd. For example on LinuxMint it returns "SSH-2.0-OpenSSH_6.0p1 Debian-3ubuntu1", the message I am looking for looks like this:

Welcome to Linux Mint 14 Nadia (GNU/Linux 3.5.0-17-generic x86_64)

Welcome to Linux Mint * Documentation: www.linuxmint.com Last login: Fri Sep 6 14:15:56 2013 from 192.168.1.38

commented Sep 10, 2013 by Tomas Knopp (58,890 points)
edited Sep 10, 2013

Yes you are right. See my newer answer below.

...