各位高手: 我现在想用C#开发一个与telnet相关的项目,需要用到telnet连接、发送指令、接受指令执行结果等等功能。
但是,我在.NET自带的库中没有发现Telnet相关的库(我用的是VS2005)。
我很奇怪啊,.NET自带有FTP这种标准协议的类库,而为什么没有Telnet相关的类库呢?像Telnet这么标准的协议,我觉得应该有标准的类库阿?!或者是我没有找到?
请各位高手指教,谢谢!
但是,我在.NET自带的库中没有发现Telnet相关的库(我用的是VS2005)。
我很奇怪啊,.NET自带有FTP这种标准协议的类库,而为什么没有Telnet相关的类库呢?像Telnet这么标准的协议,我觉得应该有标准的类库阿?!或者是我没有找到?
请各位高手指教,谢谢!
解决方案 »
- FontDialog.Font.Size的一个问题
- owssupp在线编辑word(Word2007)
- .net 调用ActiveX的问题
- vs2008中 在代码编辑器中让鼠标所在行的文字背景改变显示颜色(高亮显示)
- 求,用C#代码在Excel中画圆圈的方法!(在线等待...)
- 类型初始值设定项引发异常 dataSet
- ListView的滚动条问题,已经有人问过很多次了,但还解决不了,小弟再麻烦大家
- 多线程,集合,文件读写
- C# datagridview 与 excel 之间的操作问题,求指点。
- combox 怎么自动展开
- 获取数据库中的数据与Datagridview要输入的值进行比较
- 64位电脑中的32位winfrom程序怎么弄通过一个窗口句柄访问一个64位窗口的文件路径
{
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();
}
}
{
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();
}
}
有的 加上那个dll 就可以了
谢谢!
连接上目标主机后,发送一个命令,然后等待命令执行结果(与此同时如果屏幕上有东西返回也接受过来),待指令执行完毕后(比如出现提示符“#”等等),再执行后续其它操作。 对于你给的代码,能不能给一个使用的例子啊?
谢谢。
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();
如果不是根据结果,再发送命令的话,可以不用等待,直接把多条命令一块发过去,没问题的。
* Telnet从主机接受数据
*/public string recv_data_from_host()
http://www.dart.com/pttelnet.aspx
用TcpClient/TcpListner很容易实现的
TELNET有很多控制指令的,自己从头实现不是容易的事情。