如何实现在命令行中telnet进入某个网络设备,执行某些命令操作

elixir2002 2004-10-12 10:01:53
如何实现在命令行中telnet进入某个网络设备,执行某些命令操作
包括telnet某ip,输入密码用户,执行某些命令行的操作!!
可不可以用批处理文件方式实现,或是用c#编程实现
...全文
620 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
elixir2002 2004-10-22
  • 打赏
  • 举报
回复
我搞不懂
siugwan 2004-10-22
  • 打赏
  • 举报
回复
用SOCKET
建议参考一下登录邮件服务器接收邮件的例子.
elixir2002 2004-10-22
  • 打赏
  • 举报
回复
43s
elixir2002 2004-10-21
  • 打赏
  • 举报
回复
tian na
chinawn 2004-10-21
  • 打赏
  • 举报
回复
up
elixir2002 2004-10-13
  • 打赏
  • 举报
回复
这个程序好难看懂啊
north_star 2004-10-12
  • 打赏
  • 举报
回复
是否是要操作交换机,路由器?如果是,这个(telnet)是必须要实现的,建议你还是自己实现,可以查找 RFC 和 telnet
elixir2002 2004-10-12
  • 打赏
  • 举报
回复
谁有c#,telnet 编程的资料???
elixir2002 2004-10-12
  • 打赏
  • 举报
回复
不是的把
eradium 2004-10-12
  • 打赏
  • 举报
回复
如果你要经常批量的干这种事的话,那还是自己编程吧。
telnet协议很简单的。
eradium 2004-10-12
  • 打赏
  • 举报
回复
Cterm可以满足你的要求。
有自动登陆的脚本。在那里面加就是了。
elixir2002 2004-10-12
  • 打赏
  • 举报
回复
up
elixir2002 2004-10-12
  • 打赏
  • 举报
回复
啊这么难, luyiping(路亦平)可以详细解释一下不,我的意思不要怎么复杂,就是把固定的人工输入改成机器自动输入
luyiping 2004-10-12
  • 打赏
  • 举报
回复
当你使用命令telnet ip以后,服务器端会返回一个值
你可以再根据这个值进行下一步操作
用C#是完全可以的
具体要看TELNET的协议了
yjzhg 2004-10-12
  • 打赏
  • 举报
回复
up
elixir2002 2004-10-12
  • 打赏
  • 举报
回复
up
一半乐事 2004-10-12
  • 打赏
  • 举报
回复
public void ConnectCallback( IAsyncResult ar )
{
try
{
// Get The connection socket from the callback
Socket sock= (Socket)ar.AsyncState;
if ( sock.Connected )
{
// Define a new Callback to read the data
AsyncCallback recieveData = new AsyncCallback( OnRecievedData );
// Begin reading data asyncronously
sock.BeginReceive( m_byBuff,0,m_byBuff.Length,SocketFlags.None,recieveData ,sock);
}
}
catch( Exception ex )
{
MessageBox.Show( this, ex.Message, "Setup Recieve callbackProc failed!" );
}
}

public void OnRecievedData( IAsyncResult ar )
{
try
{
// Get The connection socket from the callback
Socket sock = (Socket)ar.AsyncState;
// Get The data , if any
int nBytesRec = sock.EndReceive(ar);
if( nBytesRec > 0 )
{
string sRecieved = Encoding.ASCII.GetString( m_byBuff, 0, nBytesRec );
string m_strLine="";
for ( int i=0; i < nBytesRec;i++)
{
Char ch = Convert.ToChar(m_byBuff[i]);
switch( ch )
{
case '\r':
m_strLine += Convert.ToString("\r\n");
break;
case '\n':
break;
default:
m_strLine += Convert.ToString(ch);
break;
}
}
try
{
int strLinelen = m_strLine.Length ;
if ( strLinelen == 0 )
{
m_strLine = Convert.ToString("\r\n");
}

Byte[] mToProcess = new Byte[strLinelen];
for ( int i=0; i < strLinelen ; i++)
{
mToProcess[i] = Convert.ToByte(m_strLine[i]);
}
// Process the incoming data
this.mOutText = ProcessOptions(mToProcess);
if ( mOutText != "" )
{
richTextBox1.AppendText(mOutText);
}
// Respond to any incoming commands
RespondToOptions();
}
catch( Exception ex )
{
//Object x = this ;
MessageBox.Show(ex.Message , "Information!" );
}
}
else
{
// If no data was recieved then the connection is probably dead
Console.WriteLine( "Disconnected", sock.RemoteEndPoint );
sock.Shutdown( SocketShutdown.Both );
sock.Close();
}
}
catch//(Exception e2)
{
//MessageBox.Show(e2.ToString());
this.Close();
}
}

private string ProcessOptions(byte[] m_strLineToProcess)
{
string m_DISPLAYTEXT ="";
string m_strTemp ="" ;
string m_strOption ="";
string m_strNormalText ="";
bool bScanDone =false;
int ndx =0;
int ldx =0;
char ch ;
try
{
for ( int i=0; i < m_strLineToProcess.Length ; i++)
{
Char ss = Convert.ToChar(m_strLineToProcess[i]);
m_strTemp = m_strTemp + Convert.ToString(ss);
}

while(bScanDone != true )
{
int lensmk = m_strTemp.Length;
ndx = m_strTemp.IndexOf(Convert.ToString(IAC));
if ( ndx > lensmk )
ndx = m_strTemp.Length;

if(ndx != -1)
{
m_DISPLAYTEXT+= m_strTemp.Substring(0,ndx);
ch = m_strTemp[ndx + 1];
if ( ch == DO || ch == DONT || ch == WILL || ch == WONT )
{
m_strOption = m_strTemp.Substring(ndx, 3);
string txt = m_strTemp.Substring(ndx + 3);
m_DISPLAYTEXT += m_strTemp.Substring(0,ndx);
m_ListOptions.Add(m_strOption);
m_strTemp = txt ;
}
else
if ( ch == IAC)
{
m_DISPLAYTEXT = m_strTemp.Substring(0,ndx);
m_strTemp = m_strTemp.Substring(ndx + 1);
}
else
if ( ch == SB )
{
m_DISPLAYTEXT = m_strTemp.Substring(0,ndx);
ldx = m_strTemp.IndexOf(Convert.ToString(SE));
m_strOption = m_strTemp.Substring(ndx, ldx);
m_ListOptions.Add(m_strOption);
m_strTemp = m_strTemp.Substring(ldx);
}
}
else
{
m_DISPLAYTEXT = m_DISPLAYTEXT + m_strTemp;
bScanDone = true ;
}
}
m_strNormalText = m_DISPLAYTEXT;
}
catch(Exception eP)
{
MessageBox.Show(eP.Message,"Application Error!!!-ProcessOptions",MessageBoxButtons.OK,MessageBoxIcon.Stop);
}
return m_strNormalText ;
}

public void DispatchMessage(string strText)
{
try
{
Byte[] smk = new Byte[strText.Length];
for ( int i=0; i < strText.Length ; i++)
{
smk[i] = Convert.ToByte(strText[i]);
}
IAsyncResult ar2 = s.BeginSend(smk,0,smk.Length,SocketFlags.None,callbackProc,s);
s.EndSend(ar2);
}
catch(Exception ers)
{
MessageBox.Show("ERROR IN RESPOND OPTIONS"+ers.ToString());
}
}

private void RespondToOptions()
{
try
{
string strOption;
for ( int i=0; i < m_ListOptions.Count; i++)
{
strOption = (string)m_ListOptions[i];
ArrangeReply(strOption);
}
DispatchMessage(m_strResp);
m_strResp ="";
m_ListOptions.Clear();
}
catch(Exception ers)
{
MessageBox.Show("ERROR IN RESPOND OPTIONS"+ers.ToString());
}
}
private void ArrangeReply(string strOption)
{
try
{
Char Verb;
Char Option;
Char Modifier;
Char ch;
bool bDefined = false;

if(strOption.Length < 3) return;

Verb = strOption[1];
Option = strOption[2];

if ( Option == 1 || Option == 3 )
{
//case 1: // Echo
//case 3: // Suppress Go-Ahead
bDefined = true;
//break;
}

m_strResp += IAC;

if(bDefined == true )
{
if ( Verb == DO )
{
ch = WILL;
m_strResp += ch;
m_strResp += Option;
}
if ( Verb == DONT )
{
ch = WONT;
m_strResp += ch;
m_strResp += Option;
}
if ( Verb == WILL )
{
ch = DO;
m_strResp += ch;
m_strResp += Option;
}
if ( Verb == WONT)
{
ch = DONT;
m_strResp += ch;
m_strResp += Option;
}
if ( Verb == SB)
{
Modifier = strOption[3];
if(Modifier == SEND)
{
ch = SB;
m_strResp += ch;
m_strResp += Option;
m_strResp += IS;
m_strResp += IAC;
m_strResp += SE;
}
}
}
else
{
if ( Verb == DO )
{
ch = WONT;
m_strResp += ch;
m_strResp += Option;
}
if ( Verb == DONT)
{
ch = WONT;
m_strResp += ch;
m_strResp += Option;
}
if ( Verb == WILL)
{
ch = DONT;
m_strResp += ch;
m_strResp += Option;
}
if ( Verb == WONT)
{
ch = DONT;
m_strResp += ch;
m_strResp += Option;
}
}
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}

private void richTextBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ( e.KeyChar == 13 )
{
DispatchMessage("\r\n");
}
else if ( e.KeyChar == 8 )
{
DispatchMessage("\b");
}
else
{
string str = e.KeyChar.ToString();
DispatchMessage(str);
}

}
}
}
一半乐事 2004-10-12
  • 打赏
  • 举报
回复
这是网上的一个程序的代码,可以参考一下。可以telnet到主机如UNIX成功,但telnet到路由器失败。正在找办法解决。
这是一个窗体的代码,窗体中只有一个richtextbox控件。
你只要将IP赋值给address ,port赋值23,然后show这个窗体就可以了。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Threading ;

namespace TelnetFliter
{
/// <summary>
/// TelnetForm 的摘要说明。
/// </summary>
public class TelnetForm : System.Windows.Forms.Form
{
/// <summary>
Char IAC = Convert.ToChar(255);
Char DO = Convert.ToChar(253);
Char DONT = Convert.ToChar(254);
Char WILL = Convert.ToChar(251);
Char WONT = Convert.ToChar(252);
Char SB = Convert.ToChar(250);
Char SE = Convert.ToChar(240);
const Char IS = '0';
const Char SEND = '1';
const Char INFO = '2';
const Char VAR = '0';
const Char VALUE = '1';
const Char ESC = '2';
const Char USERVAR = '3';
string m_strResp;

public string address ;
public int port;
public string command;
public string mOutText;

private ArrayList m_ListOptions = new ArrayList();
public IPEndPoint iep ;
public Socket s ;
private AsyncCallback callbackProc ;

Byte[] m_byBuff = new Byte[32767];
public System.Windows.Forms.RichTextBox richTextBox1;
/// </summary>
private System.ComponentModel.Container components = null;

public TelnetForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.ReadOnly = true;
this.richTextBox1.Size = new System.Drawing.Size(512, 382);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
this.richTextBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.richTextBox1_KeyPress);
//
// TelnetForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(512, 382);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.richTextBox1});
this.Name = "TelnetForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "TelnetForm";
this.Load += new System.EventHandler(this.TelnetForm_Load);
this.ResumeLayout(false);

}
#endregion

private void TelnetForm_Load(object sender, System.EventArgs e)
{
////////////////////////////////////////////////////////////////
richTextBox1.SelectionLength= 0;
//address = login_dlg.SERVER.Text;
//port = int.Parse(login_dlg.PORT.Text);
try
{
IPHostEntry IPHost = Dns.Resolve(address);
//string []aliases = IPHost.Aliases;

// Create New EndPoint
iep= new IPEndPoint(IPHost.AddressList[0],port);
// Create New Socket
s= new Socket(iep.Address.AddressFamily, SocketType.Stream,ProtocolType.Tcp);
// This is a non blocking IO
s.Blocking= false ;
// Assign Callback function to read from Asyncronous Socket
callbackProc= new AsyncCallback(ConnectCallback);
// Begin Asyncronous Connection
s.BeginConnect(iep,callbackProc,s ) ;
}
catch(Exception e1)
{
MessageBox.Show( "Application Error!!!-TelnetForm_Load"+e1.ToString());
Application.Exit();
}
}
elixir2002 2004-10-12
  • 打赏
  • 举报
回复
对我就是要操作交换机,路由器!
elixir2002 2004-10-12
  • 打赏
  • 举报
回复
天哪!!!!
加载更多回复(2)

110,590

社区成员

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

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

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