By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,908 Members | 2,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,908 IT Pros & Developers. It's quick & easy.

Using port 23 and issuing telnet commands...

P: n/a
I've Googled until my eyes hurt looking for a way to issue Telnet
commands from C# and cannot find anything but $300 libraries
that encapsulate it for you. I don't want to be able to create a
Telnet client. I just need to send a telnet request to a local IP
address on a LAN issue a "c" then a "b" and stream back the
text for internal use. The "c" changes sub-menus and the "b" is
a switch to dump the status of a firewall. I need to issue the
"b" every second and capture that stream of text that would
go to a telnet client and use it internally. Is there a way that
C# can do this programmatically? System.Diagnostices.Process
is not the solution that I am aware of. Maybe there's a switch
for it to hide the telnet window and wrap it but I couldn't find
anything to support that.

- Rex
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Rex,

this is a method I once used to execute shell commands on a unix machine.
look at the structure and I think you can start from there.

this method is old so it might not be 'elegant' or 'clean' so put those
things aside.

<Code>
public bool Exec(string Command, string Host, int Port,string UN, string
PWD)

{

TcpClient client = null;

byte[] read = new byte[128];

byte[] send = new byte[128];

int bytes;

string data;

try

{

client = new TcpClient(Host,Port);

}

catch (Exception ex) //connection problem

{

throw ex;

}

NetworkStream stream = client.GetStream();

send = Encoding.ASCII.GetBytes(UN);

stream.Write(send,0,send.Length);

bytes = stream.Read(read, 0, read.Length);

data = Encoding.ASCII.GetString(read);
send = Encoding.ASCII.GetBytes(PWD);

stream.Write(send,0,send.Length);

bytes = stream.Read(read, 0, read.Length);

data = Encoding.ASCII.GetString(read);

send = Encoding.ASCII.GetBytes(Command);

stream.Write(send,0,send.Length);
bytes = stream.Read(read, 0, read.Length);

string retval = Encoding.ASCII.GetString(read);
stream.Close();

client.Close();
if (retval[0]=='0')

return true;

else

return false;

}

</Code>

Picho

"Rex Winn" <re*@code-frog.com> wrote in message
news:15**********************@msnews.microsoft.com ...
I've Googled until my eyes hurt looking for a way to issue Telnet
commands from C# and cannot find anything but $300 libraries
that encapsulate it for you. I don't want to be able to create a
Telnet client. I just need to send a telnet request to a local IP
address on a LAN issue a "c" then a "b" and stream back the
text for internal use. The "c" changes sub-menus and the "b" is
a switch to dump the status of a firewall. I need to issue the
"b" every second and capture that stream of text that would
go to a telnet client and use it internally. Is there a way that
C# can do this programmatically? System.Diagnostices.Process
is not the solution that I am aware of. Maybe there's a switch
for it to hide the telnet window and wrap it but I couldn't find
anything to support that.

- Rex

Nov 17 '05 #2

P: n/a
I'm not one to criticize those who help and if I cannot find a solution then
I'm
a fool if I make any judgements on yours. ;) Thanks a bunch. I'll give it
a try
and see if it works.

- Rex
this method is old so it might not be 'elegant' or 'clean' so put
those things aside.


Nov 17 '05 #3

P: n/a
So I have adapted your code and it's working well enough. I think
that I'm the weak link. This is my first time writing socket level
code and working with bytes and streams so I am defitely new to
it. When I establish a connection in telnet this is the screen I get
immediately:
{OUTPUT TEXT - Telnet Console}
+-------------------------------------------------+
|+-----------------------------------------------+|
|| ||
|| DIRECWAY 6000 VSAT ||
|| ||
|| Install Console ||
|| ||
|| Dec 7 2004, 13:42:17 ||
|| ||
|| Copyright (c) 2004 Hughes Network Systems ||
|+-----------------------------------------------+|
+-------------------------------------------------+

Time of Reset : THU APR 07 09:31:08 2005
Asserted at : t=tMct /cm_data/brighton/source/usr_s2.c#403:
Reset Type : Valid Software Reset
Reset Reason : Unexpected VxWorks Exception
Main Menu (<?/CR> for options):
{/OUTPUT TEXT}

Now this is exactly 1010 bytes. However when using the code you
gave me. This is all the text I get.

{OUTPUT TEXT - Debugger}
"\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r +-------------------------------------------------+\n\r
|+-----------------------------------------------+|\n\r ||
||\n\r ||
DIRECWAY
{OUTPUT TEXT}

This is 259 bytes in length.

Why am I not getting all the output? How can I get it? I've sized the buffer
to huge and still don't get it. Ideas?

Here's the modified code:

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

namespace SigStrength
{
/// <summary>
///
/// </summary>
public class satTelnet
{
public satTelnet()
{
//
// TODO: Add constructor logic here
//
}
public bool Connect(string Command, string Host, int Port,string cmdQuestionMark,
string cmdC, string cmdG)

{
// telnet 192.168.0.1 1953
// ?
// c
// g
// repeat g
// z - return to main menu
// z - logout

TcpClient client = null;

byte[] read = new byte[1024];

byte[] send = new byte[1024];

int bytes;

string data;

try

{

client = new TcpClient(Host,Port);

}

catch (Exception ex) //connection problem

{

throw ex;

}

NetworkStream stream = client.GetStream();

send = Encoding.ASCII.GetBytes(cmdQuestionMark);

stream.Write(send,0,send.Length);

bytes = stream.Read(read, 0, read.Length);

data = Encoding.ASCII.GetString(read);
System.Diagnostics.Debug.WriteLine(data); //- DEBUG PRINT
send = Encoding.ASCII.GetBytes(cmdC);

stream.Write(send,0,send.Length);

bytes = stream.Read(read, 0, read.Length);

data = Encoding.ASCII.GetString(read);
System.Diagnostics.Debug.WriteLine(data); //- DEBUG PRINT
send = Encoding.ASCII.GetBytes(Command);

stream.Write(send,0,send.Length);

bytes = stream.Read(read, 0, read.Length);

data = Encoding.ASCII.GetString(read);
System.Diagnostics.Debug.WriteLine(data); //- DEBUG PRINT
string retval = Encoding.ASCII.GetString(read);

stream.Close();

client.Close();

if (retval[0]=='0')

return true;

else

return false;

}
}
}


Nov 17 '05 #4

P: n/a
Hi,

It may be the encoding

Do you need the response you are getting from the host?
if not just discard it

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Rex Winn" <re*@code-frog.com> wrote in message
news:15**********************@msnews.microsoft.com ...
So I have adapted your code and it's working well enough. I think
that I'm the weak link. This is my first time writing socket level
code and working with bytes and streams so I am defitely new to
it. When I establish a connection in telnet this is the screen I get
immediately:
{OUTPUT TEXT - Telnet Console}
+-------------------------------------------------+
|+-----------------------------------------------+|
|| ||
|| DIRECWAY 6000 VSAT ||
|| ||
|| Install Console ||
|| ||
|| Dec 7 2004, 13:42:17 ||
|| ||
|| Copyright (c) 2004 Hughes Network Systems ||
|+-----------------------------------------------+|
+-------------------------------------------------+

Time of Reset : THU APR 07 09:31:08 2005
Asserted at : t=tMct /cm_data/brighton/source/usr_s2.c#403:
Reset Type : Valid Software Reset
Reset Reason : Unexpected VxWorks Exception
Main Menu (<?/CR> for options):
{/OUTPUT TEXT}

Now this is exactly 1010 bytes. However when using the code you gave me.
This is all the text I get.

{OUTPUT TEXT - Debugger}
"\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r
+-------------------------------------------------+\n\r
|+-----------------------------------------------+|\n\r || ||\n\r
|| DIRECWAY
{OUTPUT TEXT}

This is 259 bytes in length.
Why am I not getting all the output? How can I get it? I've sized the
buffer
to huge and still don't get it. Ideas?

Here's the modified code:

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

namespace SigStrength
{
/// <summary>
/// /// </summary>
public class satTelnet
{
public satTelnet()
{
// // TODO: Add constructor logic here
//
}
public bool Connect(string Command, string Host, int Port,string
cmdQuestionMark, string cmdC, string cmdG)

{
// telnet 192.168.0.1 1953
// ?
// c
// g
// repeat g
// z - return to main menu
// z - logout

TcpClient client = null;

byte[] read = new byte[1024];

byte[] send = new byte[1024];

int bytes;

string data;

try

{

client = new TcpClient(Host,Port);

}

catch (Exception ex) //connection problem

{

throw ex;

}

NetworkStream stream = client.GetStream();
send = Encoding.ASCII.GetBytes(cmdQuestionMark);

stream.Write(send,0,send.Length);

bytes = stream.Read(read, 0, read.Length);

data = Encoding.ASCII.GetString(read);
System.Diagnostics.Debug.WriteLine(data); //- DEBUG PRINT
send = Encoding.ASCII.GetBytes(cmdC);

stream.Write(send,0,send.Length);

bytes = stream.Read(read, 0, read.Length);

data = Encoding.ASCII.GetString(read);
System.Diagnostics.Debug.WriteLine(data); //- DEBUG PRINT
send = Encoding.ASCII.GetBytes(Command);

stream.Write(send,0,send.Length);

bytes = stream.Read(read, 0, read.Length);

data = Encoding.ASCII.GetString(read);
System.Diagnostics.Debug.WriteLine(data); //- DEBUG PRINT
string retval = Encoding.ASCII.GetString(read);

stream.Close();

client.Close();

if (retval[0]=='0')

return true;

else

return false;

}
}
}


Nov 17 '05 #5

P: n/a
Cool! I'll give it a try.

Hi,

It may be the encoding

Do you need the response you are getting from the host? if not just
discard it

cheers,

"Rex Winn" <re*@code-frog.com> wrote in message
news:15**********************@msnews.microsoft.com ...


Nov 17 '05 #6

P: n/a
Taking a quick look at your code, you are calling Read once. However, you
maybe getting your response stream in chunks. Hence you need to keep reading
until the sender has finished sending its data. See
http://www.yoda.arachsys.com/csharp/readbinary.html for a very useful
explanation of this, and some sample code to handle this situation.

HTH
Dan

"Rex Winn" wrote:
So I have adapted your code and it's working well enough. I think
that I'm the weak link. This is my first time writing socket level
code and working with bytes and streams so I am defitely new to
it. When I establish a connection in telnet this is the screen I get
immediately:
{OUTPUT TEXT - Telnet Console}
+-------------------------------------------------+
|+-----------------------------------------------+|
|| ||
|| DIRECWAY 6000 VSAT ||
|| ||
|| Install Console ||
|| ||
|| Dec 7 2004, 13:42:17 ||
|| ||
|| Copyright (c) 2004 Hughes Network Systems ||
|+-----------------------------------------------+|
+-------------------------------------------------+

Time of Reset : THU APR 07 09:31:08 2005
Asserted at : t=tMct /cm_data/brighton/source/usr_s2.c#403:
Reset Type : Valid Software Reset
Reset Reason : Unexpected VxWorks Exception
Main Menu (<?/CR> for options):
{/OUTPUT TEXT}

Now this is exactly 1010 bytes. However when using the code you
gave me. This is all the text I get.

{OUTPUT TEXT - Debugger}
"\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r +-------------------------------------------------+\n\r
|+-----------------------------------------------+|\n\r ||
||\n\r ||
DIRECWAY
{OUTPUT TEXT}

This is 259 bytes in length.

Why am I not getting all the output? How can I get it? I've sized the buffer
to huge and still don't get it. Ideas?

Here's the modified code:

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

namespace SigStrength
{
/// <summary>
///
/// </summary>
public class satTelnet
{
public satTelnet()
{
//
// TODO: Add constructor logic here
//
}
public bool Connect(string Command, string Host, int Port,string cmdQuestionMark,
string cmdC, string cmdG)

{
// telnet 192.168.0.1 1953
// ?
// c
// g
// repeat g
// z - return to main menu
// z - logout

TcpClient client = null;

byte[] read = new byte[1024];

byte[] send = new byte[1024];

int bytes;

string data;

try

{

client = new TcpClient(Host,Port);

}

catch (Exception ex) //connection problem

{

throw ex;

}

NetworkStream stream = client.GetStream();

send = Encoding.ASCII.GetBytes(cmdQuestionMark);

stream.Write(send,0,send.Length);

bytes = stream.Read(read, 0, read.Length);

data = Encoding.ASCII.GetString(read);
System.Diagnostics.Debug.WriteLine(data); //- DEBUG PRINT
send = Encoding.ASCII.GetBytes(cmdC);

stream.Write(send,0,send.Length);

bytes = stream.Read(read, 0, read.Length);

data = Encoding.ASCII.GetString(read);
System.Diagnostics.Debug.WriteLine(data); //- DEBUG PRINT
send = Encoding.ASCII.GetBytes(Command);

stream.Write(send,0,send.Length);

bytes = stream.Read(read, 0, read.Length);

data = Encoding.ASCII.GetString(read);
System.Diagnostics.Debug.WriteLine(data); //- DEBUG PRINT
string retval = Encoding.ASCII.GetString(read);

stream.Close();

client.Close();

if (retval[0]=='0')

return true;

else

return false;

}
}
}


Nov 17 '05 #7

P: n/a
Dan,

Thanks for the links. This code helped and didn't help. What it
showed me is that I have to know the exactly byte length of the
stream coming back or it won't work. This is killing me because on
the final screen I need to read I will not know the byte length at
the time I call it. Here's the killer code block.

NetworkStream getStream = client.GetStream();
// Here's where she blows. The problem is read.Length
bytes = getStream.Read(read, 0, read.Length);
// If I could instead have the following:
bytes = getStream.Read(read, 0, getStream.Length);
// However when I attempt any operation to get the
// length of that getStream the compiler chunders out
// an error. I tried SizeOf() that didn't work. How can
// I get the size of that stream ahead of time so I can
// tell the Read command the exactly length to read?
data = Encoding.ASCII.GetString(read);
System.Diagnostics.Debug.WriteLine(data);

Taking a quick look at your code, you are calling Read once. However,
you maybe getting your response stream in chunks. Hence you need to
keep reading until the sender has finished sending its data. See
http://www.yoda.arachsys.com/csharp/readbinary.html for a very useful
explanation of this, and some sample code to handle this situation.


Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.