Using port 23 and issuing telnet commands...

R

Rex Winn

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
 
P

Picho

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
 
R

Rex Winn

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
 
R

Rex Winn

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;

}


}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

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 said:
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;

}


}
}
 
G

Guest

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 said:
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;

}


}
}
 
R

Rex Winn

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top