扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
windows系统缺省没有telnet的(这个要另外添加的)
成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、网站建设、外贸网站建设、衡水网络推广、小程序制作、衡水网络营销、衡水企业策划、衡水品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供衡水建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
所以你用system("telnet")是不行的
若你的系统已安装了telnet且在路径中,直接用
system("telnet")就可以了
//我自己用的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();
}
}
}
}
telnet的输出不在本地管道,所以你没办法重定向到文件里面,或许你用.net库函数调用PowerShell能实现(只是想法,未尝试),不知道会不会偏离你的要求太远了。
你要自己写telnet工具的话那你牛.
你可以通过system()函数调用telnet工具.
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流