0

I am writing a program with c# and .net core 3.1 which connecting to a UPS (Uninterruptible power supply) via Telnet connection. For Telnet connection I use codes from Quick tool : A minimalistic Telnet library - CodeProject .

//create a new telnet connection to hostname "gobelijn" on port "23"
TelnetConnection tc = new TelnetConnection("gobelijn", 23);

//login with user "root",password "rootpassword", using a timeout of 100ms, 
//and show server output
string s = tc.Login("root", "rootpassword",100);
Console.Write(s);

// server output should end with "$" or ">", otherwise the connection failed
string prompt = s.TrimEnd();
prompt = s.Substring(prompt.Length -1,1);
if (prompt != "$" && prompt != ">" )
    throw new Exception("Connection failed");

prompt = "";

// while connected
while (tc.IsConnected && prompt.Trim() != "exit" )
{
    // display server output
    Console.Write(tc.Read());

    // send client input to server
    prompt = Console.ReadLine();
    tc.WriteLine(prompt);

    // display server output
    Console.Write(tc.Read());
}

Console.WriteLine("***DISCONNECTED");
Console.ReadLine();

It worked for some simple one line-many parameter commands; like Settime [YYMMDD-HHmmss] that set time of the device. But When I want to execute a command like nsave that save changes in the device, command prompt of Telnet becomes like this:

>>nsave
Are you sure? (y/n)

I want to send y but I can’t. Besides that, after each command I execute exit command to disconnect Telnet connection, but in this scenario (nsave) the connection will remain open and my program cannot connect to Telnet to execute further commands. How can I include yes or other inputs in similar conditions?

  • Why are you calling "Console.Write(tc.Read()) twice. If you have only one output you will block and the programm can´t continue or do I miss something? – Darem Dec 08 '20 at 12:51
  • `Console.write(tc.Read)` is for getting data from server, What I need is somehow change `tc.WriteLine(prompt)` code. I can omit `Console.Write(tc.Read())` at all. – Hojat Afsharan Dec 08 '20 at 12:58

0 Answers0