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?