用Telnet连接到远程服务器执行命令的C#程序。
1、打开连接
2、执行命令
3、关闭连接
4、向主机发送数据
5、从主机接收数据
6、例子
using System;
using System.Text;
using System.Net.Sockets;
/// <summary>
/// Summary description for ClassTelnet
/// </summary>
public class ClassTelnet
{
TcpClient telnet_tcp_client;
public string strhost; // IP 地址
public string strusername; // username
public string strpassword; // password
private int ilogin_wait_time = 200; //网络延迟等待时间
private int irecv_wait_time = 100; //网络延迟等待时间
//Telnet protocal key
enum Verbs
{
WILL = 251,
WONT = 252,
DO = 253,
DONT = 254,
IAC = 255
}
public ClassTelnet()
{
}
/**
* Telnet 关闭连接
*/
public void close_telnet()
{
try
{
if (telnet_tcp_client == null)
{
return;
}
if (telnet_tcp_client.Connected)
{
telnet_tcp_client.Close();
}
}
catch (Exception ex)
{
Consule.Write("异常");
}
}
/**
* Telnet连接到服务器
*/
public bool open_connect()
{
bool blresult;
string strtemp;
blresult = true;
try
{
// new socket
telnet_tcp_client = new TcpClient(this.strhost, 23);
System.Threading.Thread.Sleep(ilogin_wait_time);
// read host info data