[英]Telnet server response only echoes in C# application
我需要開發一個應用程序,在使用 Telnet 連接到 ip 后能夠發出一些命令,然后只記錄正在進行的響應;
所以我嘗試了 package 之類的 PrimS.Telnet 和 Minimalistic.Telnet; 問題是它適用於其他 telnet 服務器,但不適用於這個服務器; 我得到的只是大寫的回聲:
當我使用膩子(我無法自動化)時,它確實給出了正確的響應:
我必須先按一個輸入,然后才能消除那個奇怪的故障字符
這是正常的嗎? 我在這里遺漏了什么,為什么我不能在此服務器上使用我的 C# 應用程序?
編輯 1:我已經發現我的 C# 不支持某些要求不回顯文本的 telnet 命令(請參閱Telnet 命令)。 所以我的問題是如何解析這些 telnet 命令以便發送它們?
好的小例子給你。 方法AskReceive
發送一個命令並等待 200 英里秒的響應。 它使用 Stream 進行發送和接收。 如果您發送clearTextWriter.WriteLine(commandline)
您正在向您的設備發送字符串命令。
using System;
using System.IO;
using System.Net.Sockets;
namespace CommonCore.Classes.Helper
{
class TelnetDevice01
{
static int connectionTimeout = 1300;
string AskReceive(string commandline, ref string _log)
{
_log += "> " + commandline + Environment.NewLine;
clearTextWriter.WriteLine(commandline);
string _str;
System.Threading.Thread.Sleep(200);
_str = clearTextReader.ReadLine();
_log += "< " + _str + Environment.NewLine;
return _str;
}
void ExitError(string str, ref string _log, ref string _error)
{
_error = str;
_log += "!! Error : " + str + Environment.NewLine + Environment.NewLine;
clearTextWriter.WriteLine("QUIT");
}
StreamReader clearTextReader = null;
StreamWriter clearTextWriter = null;
public void ConnectTelnet(string login, string password, string server, int port,
out string log, out string resume, out string error
)
{
string _response = "";
resume = "";
error = "";
log = "";
TcpClient client = new TcpClient();
//Make the connection with timeout
if (!client.ConnectAsync(server, port).Wait(connectionTimeout))
{
//log = ex.ExceptionToString();
error = $"Could not connect '{server}' at port '{port}'";
log += Environment.NewLine + error + Environment.NewLine;
resume = Environment.NewLine + $"[FAIL] Port={port}. Could not connect '{server}' at port '{port}'" + Environment.NewLine;
return;
}
using (client)
{
using (NetworkStream stream = client.GetStream())
using (clearTextReader = new StreamReader(stream))
using (clearTextWriter = new StreamWriter(stream) { AutoFlush = true })
{
log += Environment.NewLine + Environment.NewLine + "## Connected" + Environment.NewLine;
//Read the start response line like "User:" ?'
string connectResponse = clearTextReader.ReadLine();
log += "< " + connectResponse + Environment.NewLine;
if (!connectResponse.StartsWith("login"))
{
ExitError(_response, ref log, ref error);
resume = Environment.NewLine + $"Expecting 'login'";
return;
}
//Send login
if (!(_response = AskReceive(login, ref log)).StartsWith("password"))
{
ExitError(_response, ref log, ref error);
resume = Environment.NewLine + $"Asnswer should have been 'password'";
return;
}
// Is asking for password, let's send the pass now
if (!(_response = AskReceive(password, ref log)).StartsWith("Login OK"))
{
ExitError(_response, ref log, ref error);
resume = Environment.NewLine + $"Answer should have been 'Login OK'";
return;
}
//Send CMD SMDR
_response = AskReceive($"SMDR", ref log);
//Check if the answer is what you want
// like _response.Contains("blabla")
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.