473,387 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using port 23 and issuing telnet commands...

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
7 14054
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dan | last post by:
I'm writing a simplistic telnet client in VB6 and I've run into a small snag. The program has a textbox to write in the string to be sent using ..SendData and has another textbox that displays...
3
by: Yannick Turgeon | last post by:
Hello all, I'm currently trying to pass commands to a telnet session and get the texte generated (stdin + stdout) by the session. The problem I get is that the Telnet.read_until() function...
3
by: Steve Koon | last post by:
Anyone know of a C# telnet server code that I can look at. I want to integrate a telnet server function inside a application that I am writing. Thanks,
5
by: Bob Garbados | last post by:
I have a .net website and I need to connect to a service listening on a specific port from that website. The service and website are on the same windows 03 server. I don't know where to start on...
0
by: knowthediff | last post by:
Hello, I have a VS 2005 windows form app that I am creating. It uses Async socket to telnet which sends and receives commands. Everything seems to be working fine until I attempt to process a...
20
by: valpa | last post by:
I'm a net admin for about 20 unix servers, and I need to frequently telnet on to them and configure them. It is a tiring job to open a xterm and telnet, username, password to each server. Can I...
7
by: oopsbabies | last post by:
Hello everyone, I am using Apache 1.3.33 as the web server and PHP version 4.3.10. My machine is using Windows XP 2002 professional edition which comes with a Windows firewall. I am using McAfee...
4
by: Eran.Yasso | last post by:
Hi all, I am trying to write application which runs on my PC that sends data to an application running on Windows mobile. The problem is that the tcp over activesync doesn't work. The device is...
14
by: Warren Tang | last post by:
Hi I am using the mail function to send a mail like this: $b = mail("my_real_email_address@gmail.com", "Hello from PHP", "Hi, finally sent an email successfully"); But it failed. Could you...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.