请问.NET有没有自带的Telnet库?

OOPhaisky 2012-02-20 11:12:18
各位高手:

   我现在想用C#开发一个与telnet相关的项目,需要用到telnet连接、发送指令、接受指令执行结果等等功能。
   但是,我在.NET自带的库中没有发现Telnet相关的库(我用的是VS2005)。
   我很奇怪啊,.NET自带有FTP这种标准协议的类库,而为什么没有Telnet相关的类库呢?像Telnet这么标准的协议,我觉得应该有标准的类库阿?!或者是我没有找到?
   请各位高手指教,谢谢!
...全文
813 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
crackdung 2012-02-21
  • 打赏
  • 举报
回复
參考一下 http://www.dart.com/pttelnet.aspx
crackdung 2012-02-20
  • 打赏
  • 举报
回复
PowerTCP Telnet 不錯
段传涛 2012-02-20
  • 打赏
  • 举报
回复
回答的果然够长。
有的 加上那个dll 就可以了
wushuai1346 2012-02-20
  • 打赏
  • 举报
回复
System.Net里面不知道有没有
OOPhaisky 2012-02-20
  • 打赏
  • 举报
回复
to wy811007:

请问,如何用C#操控系统自带的telnet啊?
wy811007 2012-02-20
  • 打赏
  • 举报
回复
囧 我之前写过一个软件 操控系统的telnet 其实完全可以用系统自带的啊
baichangle 2012-02-20
  • 打赏
  • 举报
回复
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
{
myer += "异常\n";
}
}

/**
* 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
strtemp = recv_data_from_host();
blresult = strtemp.TrimEnd().EndsWith(":");
if (blresult == false)
{
myer += "read host info data error \n";
return blresult;
}

// username send to host
blresult = send_data_to_host(this.strusername + "\n\r");
if (blresult == false)
{
myer += "username send error\n";
return blresult;
}

System.Threading.Thread.Sleep(ilogin_wait_time);
strtemp = recv_data_from_host();
blresult = strtemp.TrimEnd().EndsWith(":");
if (blresult == false)
{
return blresult;
}

// password send to host
blresult = send_data_to_host(this.strpassword + "\n\r");
if (blresult == false)
{
return blresult;
}
System.Threading.Thread.Sleep(ilogin_wait_time);
strtemp = recv_data_from_host();
if ((strtemp.Trim().LastIndexOf("#") > -1) ||
(strtemp.Trim().LastIndexOf("$") > -1) ||
(strtemp.Trim().LastIndexOf(">") > -1))
{
blresult = true;
}
else
{
blresult = false;
}
}
catch
{
blresult = false;
}
return blresult;
}

/**
* 执行命令
*/
public bool exec_command(string strcmd)
{
bool blresult;
string strprompt;

blresult = false;
strprompt = "";

if (telnet_tcp_client.Connected)
{
blresult = send_data_to_host(strcmd + "\n\r");
if (blresult == false)
{
return false;
}
strprompt = "";

strprompt = recv_data_from_host();

if ((strprompt.Trim().LastIndexOf("#") > -1) ||
(strprompt.Trim().LastIndexOf("$") > -1) ||
(strprompt.Trim().LastIndexOf(">") > -1))
{
blresult = true;
return blresult;
}
}
return blresult;
}

/**
* telnet向主机发送数据
*/
public bool send_data_to_host(string strcmd)
{
try
{
// socket error时、return
if (!telnet_tcp_client.Connected)
{
return false;
}

byte[] bbuf = System.Text.ASCIIEncoding.ASCII.GetBytes(strcmd.Replace("\0xFF", "\0xFF\0xFF"));

telnet_tcp_client.GetStream().Write(bbuf, 0, bbuf.Length);
}
catch
{
return false;
}
return true;
}

/**
* Telnet从主机接受数据
*/
public string recv_data_from_host()
{
int iinput_data; //data
int inputverb;
int inputoption;
StringBuilder sbtemp;
NetworkStream ns_temp;
byte[] bread_buffer;
StringBuilder sbcomplete_message;
int iread_bytes_num;

sbtemp = new StringBuilder();

// socket没有连接的时候,返回空
if (!telnet_tcp_client.Connected)
{
return null;
}

do
{
// read 1 byte
iinput_data = telnet_tcp_client.GetStream().ReadByte();
switch (iinput_data)
{
case -1:
break;
case (int)Verbs.IAC: // 接受的数据有keyword

// read 1 byte
inputverb = telnet_tcp_client.GetStream().ReadByte();
if (inputverb == -1) break;
switch (inputverb)
{
case (int)Verbs.IAC:
sbtemp.Append(inputverb);
break;
case (int)Verbs.DO:
case (int)Verbs.DONT:
case (int)Verbs.WILL:
case (int)Verbs.WONT:
inputoption = telnet_tcp_client.GetStream().ReadByte();
if (inputoption == -1) break;
telnet_tcp_client.GetStream().WriteByte((byte)Verbs.IAC);
telnet_tcp_client.GetStream().WriteByte(inputverb ==
(int)Verbs.DO ? (byte)Verbs.WONT : (byte)Verbs.DONT);
telnet_tcp_client.GetStream().WriteByte((byte)inputoption);
break;
default:
break;
}
break;
default:
sbtemp.Append((char)iinput_data);
bread_buffer = new byte[8192];
sbcomplete_message = new StringBuilder();
iread_bytes_num = 0;
ns_temp = telnet_tcp_client.GetStream();
if (ns_temp.CanRead)
{
System.Threading.Thread.Sleep(ilogin_wait_time);
iread_bytes_num = ns_temp.Read(bread_buffer, 0, bread_buffer.Length);
sbtemp.AppendFormat("{0}", Encoding.ASCII.GetString(bread_buffer,
0, iread_bytes_num));
}
break;
}

// timeout
System.Threading.Thread.Sleep(irecv_wait_time);
} while (telnet_tcp_client.Available > 0);

// 返回接受的数据
return sbtemp.ToString();
}
}
baichangle 2012-02-20
  • 打赏
  • 举报
回复
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
{
myer += "异常\n";
}
}

/**
* 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
strtemp = recv_data_from_host();
blresult = strtemp.TrimEnd().EndsWith(":");
if (blresult == false)
{
myer += "read host info data error \n";
return blresult;
}

// username send to host
blresult = send_data_to_host(this.strusername + "\n\r");
if (blresult == false)
{
myer += "username send error\n";
return blresult;
}

System.Threading.Thread.Sleep(ilogin_wait_time);
strtemp = recv_data_from_host();
blresult = strtemp.TrimEnd().EndsWith(":");
if (blresult == false)
{
return blresult;
}

// password send to host
blresult = send_data_to_host(this.strpassword + "\n\r");
if (blresult == false)
{
return blresult;
}
System.Threading.Thread.Sleep(ilogin_wait_time);
strtemp = recv_data_from_host();
if ((strtemp.Trim().LastIndexOf("#") > -1) ||
(strtemp.Trim().LastIndexOf("$") > -1) ||
(strtemp.Trim().LastIndexOf(">") > -1))
{
blresult = true;
}
else
{
blresult = false;
}
}
catch
{
blresult = false;
}
return blresult;
}

/**
* 执行命令
*/
public bool exec_command(string strcmd)
{
bool blresult;
string strprompt;

blresult = false;
strprompt = "";

if (telnet_tcp_client.Connected)
{
blresult = send_data_to_host(strcmd + "\n\r");
if (blresult == false)
{
return false;
}
strprompt = "";

strprompt = recv_data_from_host();

if ((strprompt.Trim().LastIndexOf("#") > -1) ||
(strprompt.Trim().LastIndexOf("$") > -1) ||
(strprompt.Trim().LastIndexOf(">") > -1))
{
blresult = true;
return blresult;
}
}
return blresult;
}

/**
* telnet向主机发送数据
*/
public bool send_data_to_host(string strcmd)
{
try
{
// socket error时、return
if (!telnet_tcp_client.Connected)
{
return false;
}

byte[] bbuf = System.Text.ASCIIEncoding.ASCII.GetBytes(strcmd.Replace("\0xFF", "\0xFF\0xFF"));

telnet_tcp_client.GetStream().Write(bbuf, 0, bbuf.Length);
}
catch
{
return false;
}
return true;
}

/**
* Telnet从主机接受数据
*/
public string recv_data_from_host()
{
int iinput_data; //data
int inputverb;
int inputoption;
StringBuilder sbtemp;
NetworkStream ns_temp;
byte[] bread_buffer;
StringBuilder sbcomplete_message;
int iread_bytes_num;

sbtemp = new StringBuilder();

// socket没有连接的时候,返回空
if (!telnet_tcp_client.Connected)
{
return null;
}

do
{
// read 1 byte
iinput_data = telnet_tcp_client.GetStream().ReadByte();
switch (iinput_data)
{
case -1:
break;
case (int)Verbs.IAC: // 接受的数据有keyword

// read 1 byte
inputverb = telnet_tcp_client.GetStream().ReadByte();
if (inputverb == -1) break;
switch (inputverb)
{
case (int)Verbs.IAC:
sbtemp.Append(inputverb);
break;
case (int)Verbs.DO:
case (int)Verbs.DONT:
case (int)Verbs.WILL:
case (int)Verbs.WONT:
inputoption = telnet_tcp_client.GetStream().ReadByte();
if (inputoption == -1) break;
telnet_tcp_client.GetStream().WriteByte((byte)Verbs.IAC);
telnet_tcp_client.GetStream().WriteByte(inputverb ==
(int)Verbs.DO ? (byte)Verbs.WONT : (byte)Verbs.DONT);
telnet_tcp_client.GetStream().WriteByte((byte)inputoption);
break;
default:
break;
}
break;
default:
sbtemp.Append((char)iinput_data);
bread_buffer = new byte[8192];
sbcomplete_message = new StringBuilder();
iread_bytes_num = 0;
ns_temp = telnet_tcp_client.GetStream();
if (ns_temp.CanRead)
{
System.Threading.Thread.Sleep(ilogin_wait_time);
iread_bytes_num = ns_temp.Read(bread_buffer, 0, bread_buffer.Length);
sbtemp.AppendFormat("{0}", Encoding.ASCII.GetString(bread_buffer,
0, iread_bytes_num));
}
break;
}

// timeout
System.Threading.Thread.Sleep(irecv_wait_time);
} while (telnet_tcp_client.Available > 0);

// 返回接受的数据
return sbtemp.ToString();
}
}
OOPhaisky 2012-02-20
  • 打赏
  • 举报
回复
to nonocast:

TELNET有很多控制指令的,自己从头实现不是容易的事情。
nonocast 2012-02-20
  • 打赏
  • 举报
回复
Telnet的本质就是scoket
用TcpClient/TcpListner很容易实现的
OOPhaisky 2012-02-20
  • 打赏
  • 举报
回复
to baichangle:

我想实现“给目标主机发送指令后马上接收该指令的执行过程和结果”,请问这样如何实现?麻烦给我写一下代码,谢谢。
crackdung 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 oophaisky 的回复:]

to crackdung:

请问,“PowerTCP Telnet”是.NET类库么?如果可以在.NET中使用,能不能给一个下载地址和使用说明之类的东西?
谢谢!
[/Quote]

http://www.dart.com/pttelnet.aspx
baichangle 2012-02-20
  • 打赏
  • 举报
回复
/**
* Telnet从主机接受数据
*/

public string recv_data_from_host()
OOPhaisky 2012-02-20
  • 打赏
  • 举报
回复
to baichangle:

我需要接收命令执行的过程以及结果数据,可能会对这些数据进行一些简单的分析,请问应该如何做?
baichangle 2012-02-20
  • 打赏
  • 举报
回复
ClassTelnet myt = new ClassTelnet();
myt.strhost = "192.168.1.1";
myt.strusername = "root";
myt.strpassword = "root";
myt.open_connect();
myt.exec_command("killall -9 serv1");
myt.exec_command("killall -9 serv2");
myt.close_telnet();
如果不是根据结果,再发送命令的话,可以不用等待,直接把多条命令一块发过去,没问题的。
OOPhaisky 2012-02-20
  • 打赏
  • 举报
回复
to baichangle:

我想要实现的功能是:
连接上目标主机后,发送一个命令,然后等待命令执行结果(与此同时如果屏幕上有东西返回也接受过来),待指令执行完毕后(比如出现提示符“#”等等),再执行后续其它操作。

对于你给的代码,能不能给一个使用的例子啊?
谢谢。
OOPhaisky 2012-02-20
  • 打赏
  • 举报
回复
to crackdung:

请问,“PowerTCP Telnet”是.NET类库么?如果可以在.NET中使用,能不能给一个下载地址和使用说明之类的东西?
谢谢!

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧