Telnet c# not all output returned

0 votes
asked Jul 27, 2012 by thorst (160 points)
edited Aug 1, 2012

I have a relatively simple scenario. I need to telnet into a UNIX server. Issue a command. And receive all the output from said command.

I used the below function to log on, issue a simple command, get its output, then issue my more complicated command, and retrieve its output.

The Problem: Not all the output is returned from "myunixcommand '/my path to file/for param for command'". I get the last 19 lines of output, instead of the 70 some lines of actual output. There are a couple crlf (0d0a) in the output, im not sure if thats the issue?

I tried two methods to retrieving all the output, both return the same thing.

Any ideas?

 public static void test4(string server, string un, string pw)
    {
        Telnet client = new Telnet(server);
        Shell shell = client.StartShell();
        shell.Prompt = "$ ";
        string response = shell.ReadAll("ogin: ");
        shell.SendCommand(un);
        response = shell.ReadAll("ssword: ");
        shell.SendCommand(pw, true);
        string loginMessage = shell.ReadAll();

        //send some easy command i know will work
        shell.SendCommand("echo test");
        loginMessage = shell.ReadAll();

        //try a command with lots of output
        shell.SendCommand("myunixcommand '/my path to file/for param for command'", true);
        //Try #1
        //loginMessage = "";
        //while (shell.IsRunning)
        //{
        //    loginMessage += result;
        //}

        //Try #2
        loginMessage = shell.ReadAll();

        // close the shell
        shell.Close();
    }

1 Answer

0 votes
answered Aug 1, 2012 by Lukas Matyska (59,530 points)
edited Aug 1, 2012

Hello,

You sent us an email with the question also. So I would like to solve the problem using the email conversation and then post the final answer on this forum. But I didn't receive any reply to my mail, so I am posting it here:

Can you please update your code as shown below and send the produced log file to support@rebex.net? I can spot something important (I suspect the short prompt "$ " = it can be present in the command's output).

 Telnet client = new Telnet(server);
 Shell shell = client.StartShell();
 shell.Prompt = "$ ";
 string response = shell.ReadAll("ogin: ");
 shell.SendCommand(un);
 response = shell.ReadAll("ssword: ");
 shell.SendCommand(pw, true);
 string loginMessage = shell.ReadAll();

 //enable logging
 Rebex.FileLogWriter logger = new Rebex.FileLogWriter(@"C:\temp\telnet.log", Rebex.LogLevel.Verbose);
 client.LogWriter = logger;

 //try a command with lots of output
 shell.SendCommand("myunixcommand '/my path to file/for param for command'", true);
 do
 {
       response = shell.ReadAll();
 } while (response != null);

 // close the shell
 shell.Close();
 logger.Close();

Or have you solved the problem?

...