これは簡単ですか?良い例はありますか?私のすべてのグーグル検索は、dotNetでtelnetクライアントを作成する方法に関するアイテムを返しますが、これは私にとってやり過ぎです。私はC#でこれをやろうとしています。
ありがとう!
C# 2.0 と Telnet - 思ったほど苦痛では
ない
または、この代替リンク.
System.Net.Sockets クラスを使用する場合は、次のようにします。
ProtocolType.Tcp を使用するように構成されたソケット パラメータが既にあるソケット オブジェクトの代わりに、System.Net.Sockets.TcpClient オブジェクトを使用することもできます。それでは、そのオプションについて説明しましょう。
単純なタスク (telnet のようなインターフェースを備えた特殊なハードウェア デバイスへの接続など) の場合は、ソケットを介して接続し、テキスト コマンドを送受信するだけで十分です。
実際の telnet サーバーに接続する場合は、telnet エスケープ シーケンスの処理、ターミナル エミュレーションの処理、対話型コマンドの処理などが必要になる場合があります。CodeProject の Minimalistic Telnet ライブラリ(無料) や市販の Telnet/Terminal Emulator ライブラリなど、既にテスト済みのコードを使用する( Rebex Telnetなど) を使用すると、時間を節約できます。
次のコード (この URLから取得) は、その使用方法を示しています。
// create the client
Telnet client = new Telnet("servername");
// start the Shell to send commands and read responses
Shell shell = client.StartShell();
// set the prompt of the remote server's shell first
shell.Prompt = "servername# ";
// read a welcome message
string welcome = shell.ReadAll();
// display welcome message
Console.WriteLine(welcome);
// send the 'df' command
shell.SendCommand("df");
// read all response, effectively waiting for the command to end
string response = shell.ReadAll();
// display the output
Console.WriteLine("Disk usage info:");
Console.WriteLine(response);
// close the shell
shell.Close();