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 programmaticall y? System.Diagnost ices.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 7 14096
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.GetStrea m();
send = Encoding.ASCII. GetBytes(UN);
stream.Write(se nd,0,send.Lengt h);
bytes = stream.Read(rea d, 0, read.Length);
data = Encoding.ASCII. GetString(read) ;
send = Encoding.ASCII. GetBytes(PWD);
stream.Write(se nd,0,send.Lengt h);
bytes = stream.Read(rea d, 0, read.Length);
data = Encoding.ASCII. GetString(read) ;
send = Encoding.ASCII. GetBytes(Comman d);
stream.Write(se nd,0,send.Lengt h);
bytes = stream.Read(rea d, 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.microsof t.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 programmaticall y? System.Diagnost ices.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
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.
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.Sock ets;
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.GetStrea m();
send = Encoding.ASCII. GetBytes(cmdQue stionMark);
stream.Write(se nd,0,send.Lengt h);
bytes = stream.Read(rea d, 0, read.Length);
data = Encoding.ASCII. GetString(read) ;
System.Diagnost ics.Debug.Write Line(data); //- DEBUG PRINT
send = Encoding.ASCII. GetBytes(cmdC);
stream.Write(se nd,0,send.Lengt h);
bytes = stream.Read(rea d, 0, read.Length);
data = Encoding.ASCII. GetString(read) ;
System.Diagnost ics.Debug.Write Line(data); //- DEBUG PRINT
send = Encoding.ASCII. GetBytes(Comman d);
stream.Write(se nd,0,send.Lengt h);
bytes = stream.Read(rea d, 0, read.Length);
data = Encoding.ASCII. GetString(read) ;
System.Diagnost ics.Debug.Write Line(data); //- DEBUG PRINT
string retval = Encoding.ASCII. GetString(read) ;
stream.Close();
client.Close();
if (retval[0]=='0')
return true;
else
return false;
}
}
}
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.microsof t.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.Sock ets; 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.GetStrea m(); send = Encoding.ASCII. GetBytes(cmdQue stionMark);
stream.Write(se nd,0,send.Lengt h);
bytes = stream.Read(rea d, 0, read.Length);
data = Encoding.ASCII. GetString(read) ; System.Diagnost ics.Debug.Write Line(data); //- DEBUG PRINT send = Encoding.ASCII. GetBytes(cmdC);
stream.Write(se nd,0,send.Lengt h);
bytes = stream.Read(rea d, 0, read.Length);
data = Encoding.ASCII. GetString(read) ; System.Diagnost ics.Debug.Write Line(data); //- DEBUG PRINT send = Encoding.ASCII. GetBytes(Comman d);
stream.Write(se nd,0,send.Lengt h);
bytes = stream.Read(rea d, 0, read.Length);
data = Encoding.ASCII. GetString(read) ; System.Diagnost ics.Debug.Write Line(data); //- DEBUG PRINT string retval = Encoding.ASCII. GetString(read) ;
stream.Close();
client.Close();
if (retval[0]=='0')
return true;
else
return false;
}
} }
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.microsof t.com...
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.Sock ets; 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.GetStrea m();
send = Encoding.ASCII. GetBytes(cmdQue stionMark);
stream.Write(se nd,0,send.Lengt h);
bytes = stream.Read(rea d, 0, read.Length);
data = Encoding.ASCII. GetString(read) ; System.Diagnost ics.Debug.Write Line(data); //- DEBUG PRINT send = Encoding.ASCII. GetBytes(cmdC);
stream.Write(se nd,0,send.Lengt h);
bytes = stream.Read(rea d, 0, read.Length);
data = Encoding.ASCII. GetString(read) ; System.Diagnost ics.Debug.Write Line(data); //- DEBUG PRINT send = Encoding.ASCII. GetBytes(Comman d);
stream.Write(se nd,0,send.Lengt h);
bytes = stream.Read(rea d, 0, read.Length);
data = Encoding.ASCII. GetString(read) ; System.Diagnost ics.Debug.Write Line(data); //- DEBUG PRINT string retval = Encoding.ASCII. GetString(read) ;
stream.Close();
client.Close();
if (retval[0]=='0')
return true;
else
return false;
}
} }
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.GetStrea m();
// 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.Lengt h);
// 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.Diagnost ics.Debug.Write Line(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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 what that server sends.
When I first connect to the server (in this case, my university's smtp
server), I get a response that the server acknowledges my connection. When
I type something into the textbox and send it, however, I get no response...
|
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 seems to freeze after a couple of command. I
did a simplify script that reproduce the problem each time (I'm using 2.3.4
on W2K):
|
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,
|
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 this, can someone point
me in the right direction.
Thanks.
|
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 command quickly. For
example, I want to creata a script and run that script on the telnet
window. For simpliciy lets say I would like to list directory contents
2 times in a row. I want to process "dir" two times.
So, via code I send the dir...
| |
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 do it automatically by python? After that, there have 20 xterm
consoles opened and telneted to their corresponding servers. Then I
could start to type command in these xterms.
Any suggestion appreciate. Much thanks.
|
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 free edition for anti-virus.
I use an ISP provider from my country and according to them I do not
need to perform authentication while sending mails through their SMTP
address. Thus I am using external SMTP server. I don't have IIS
installed...
|
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 not connected to ehternet.
Can I send tcp traffic to device using active sync?
I sas that the device's IPaddress is 192.168.55.101. I changed my PC
IP address to 192.168.55.100 and sent ping but didn't get any ping
reply.
|
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 guide me to get it work?
Regards
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |