C# winform点击按钮执行telnet登陆,并且执行命令,返回结果,求代码示例。 20
1个回答
展开全部
//我自己用的telnetHelper类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace AFC_APP1
{
public class TelnetHelper2
{
string hostName;
string login;
string password;
int port = 23;
IPAddress ipAdd;
IPEndPoint ipEnd;
Socket mysocket;
/// <summary>
/// 默认构造函数
/// </summary>
public TelnetHelper2()
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="hostName"></param>
/// <param name="login"></param>
/// <param name="password"></param>
public TelnetHelper2(string hostName,string login,string password)
{
this.hostName = hostName;
this.login = login;
this.password = password;
ipAdd=IPAddress.Parse(hostName);
ipEnd = new IPEndPoint(ipAdd, port);
mysocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
/// <summary>
/// 连接
/// </summary>
private void ConnetServer()
{
char[] sendfirst = new char[3];
sendfirst[0] = (char)(255);
sendfirst[1] = (char)(252);
sendfirst[2] = (char)(24);
byte[] sendfirstbytetemp = Encoding.Unicode.GetBytes(sendfirst);
byte[] sendfirstbyte = new byte[sendfirstbytetemp.Length / 2];
sendfirstbyte[0] = sendfirstbytetemp[0];
sendfirstbyte[1] = sendfirstbytetemp[2];
sendfirstbyte[2] = sendfirstbytetemp[4];
mysocket.Send(sendfirstbyte, 0, sendfirstbyte.Length, SocketFlags.None);
Thread.Sleep(300);
byte[] sendusernametemp = Encoding.ASCII.GetBytes(login);
byte[] sendusername = new byte[sendusernametemp.Length + 1];
char[] closechar = new char[2];
closechar[0] = (char)(10);
closechar[1] = (char)(13);
byte[] closebyte = Encoding.ASCII.GetBytes(closechar);
for (int i = 0; i < sendusernametemp.Length; i++)
{
sendusername[i] = sendusernametemp[i];
}
sendusername[sendusername.Length - 1] = closebyte[0];
mysocket.Send(sendusername, 0, sendusername.Length, SocketFlags.None);
Thread.Sleep(300);
byte[] senduserpasswdtemp = Encoding.ASCII.GetBytes(password);
byte[] senduserpasswd = new byte[senduserpasswdtemp.Length + 1];
char[] closechar2 = new char[2];
closechar2[0] = (char)(10);
closechar2[1] = (char)(13);
byte[] closebyte2 = Encoding.ASCII.GetBytes(closechar2);
for (int i = 0; i < senduserpasswdtemp.Length; i++)
{
senduserpasswd[i] = senduserpasswdtemp[i];
}
senduserpasswd[senduserpasswd.Length - 1] = closebyte2[0];
mysocket.Send(senduserpasswd, 0, senduserpasswd.Length, SocketFlags.None);
Thread.Sleep(500);
}
/// <summary>
/// 执行命令
/// </summary>
/// <param name="cmd"></param>
private void ExcuteCommand(string cmd)
{
char[] sendfirst = new char[3];
sendfirst[0] = (char)(255);
sendfirst[1] = (char)(252);
sendfirst[2] = (char)(24);
byte[] sendfirstbytetemp = Encoding.Unicode.GetBytes(sendfirst);
byte[] sendfirstbyte = new byte[sendfirstbytetemp.Length / 2];
sendfirstbyte[0] = sendfirstbytetemp[0];
sendfirstbyte[1] = sendfirstbytetemp[2];
sendfirstbyte[2] = sendfirstbytetemp[4];
mysocket.Send(sendfirstbyte, 0, sendfirstbyte.Length, SocketFlags.None);
Thread.Sleep(300);
byte[] recvdatauser = new byte[4096];
mysocket.Receive(recvdatauser, 0, mysocket.Available, SocketFlags.None);
string recvuserstr = Encoding.ASCII.GetString(recvdatauser);
recvuserstr = recvuserstr.Substring(login.Length, recvuserstr.Length - login.Length);//去掉了服务器回显的用户名
recvuserstr = recvuserstr.TrimEnd('\0');
Console.Write(recvuserstr);
Thread.Sleep(300);
byte[] sendusernametemp = Encoding.ASCII.GetBytes(cmd);
byte[] sendusername = new byte[sendusernametemp.Length + 1];
char[] closechar = new char[2];
closechar[0] = (char)(10);
closechar[1] = (char)(13);
byte[] closebyte = Encoding.ASCII.GetBytes(closechar);
for (int i = 0; i < sendusernametemp.Length; i++)
{
sendusername[i] = sendusernametemp[i];
}
sendusername[sendusername.Length - 1] = closebyte[0];
mysocket.Send(sendusername, 0, sendusername.Length, SocketFlags.None);
Thread.Sleep(500);
}
/// <summary>
/// 获取数据
/// </summary>
/// <returns></returns>
public string GetData(string cmd)
{
try
{
mysocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
mysocket.Connect(ipEnd);
ConnetServer(); //连接服务
ExcuteCommand(cmd);//执行命令
byte[] recvdatauser = new byte[8192];
mysocket.Receive(recvdatauser, 0, mysocket.Available, SocketFlags.None);
string recvuserstr = Encoding.ASCII.GetString(recvdatauser);
recvuserstr = recvuserstr.TrimEnd('\0');
mysocket.Close();
System.Threading.Thread.Sleep(300);
//Console.Write("recvuserstr_string:" + recvuserstr);
recvuserstr = recvuserstr.Substring(cmd.Length, recvuserstr.Length - cmd.Length).TrimStart();//去掉了服务器回显
recvuserstr = this.login.Equals("root") ? recvuserstr.Replace("#", "").TrimEnd() : recvuserstr.Replace("$", "").TrimEnd();
return recvuserstr;//.Split('\r')[1].Trim();
}
catch (Exception ex)
{
LogHelper.WriteTelnetLog(ex.ToString());
return "EXError:"+ex.ToString();
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询