IP connections

H

Hans Vlems

I'm asked to write a program that runs on a Windows or Linux host and
must set up a telnet connection to a network device. This network
device understands very basic commands (one or two letters) and
produces output, depending on the command. The proud owner of this hi-
tech device doesn't want to initiate a session from the command prompt
but rather run a program that issues the same command(-sequence) every
day and collects the output of the remote device.
My first idea was to have the C program build a command script and
start that script with the system() function.
Looks simple, but system() runs its script asynchronously, i.e. the
originating program has no control over the script.
Second idea: connect to the telnet port of the hi-tech device in the C
program. I'm a newbie in TCP/IP programming so there are a few
questions:
- is there a library with handy functions that I can use (Windows
based to start with)?
- does a network connection behave like a file on disk that can may be
read from or written to?
- a pointer to a book that explains these issues (preferably with easy
to understand pictures ;-)?
Hans
 
H

Hans Vlems

If you can run on Linux, then just make your telnet script and run it
with a cron job.  "expect" might be useful here.

If you need more programmability, it is a pointless waste of time to use
C for something like this.  Use a higher level language that supports
scripting and has appropriate libraries (as well as plenty of examples
around the web).  If you already have a favourite, such as perl, python,
tcl, lua, etc., then use that.  If you are starting from zero, then I'd
recommend Python with the "telnetlib" library:

<http://docs.python.org/library/telnetlib.html>

Even if you have never heard of Python, you will get the job done faster
and more reliably with Python than coding in C.- Tekst uit oorspronkelijkbericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

David, I'll look into Python even though I've never seen a line of
Python in my life.
 
A

Angel

I'm asked to write a program that runs on a Windows or Linux host and
must set up a telnet connection to a network device. This network
device understands very basic commands (one or two letters) and
produces output, depending on the command. The proud owner of this hi-
tech device doesn't want to initiate a session from the command prompt
but rather run a program that issues the same command(-sequence) every
day and collects the output of the remote device.
My first idea was to have the C program build a command script and
start that script with the system() function.
Looks simple, but system() runs its script asynchronously, i.e. the
originating program has no control over the script.
Second idea: connect to the telnet port of the hi-tech device in the C
program. I'm a newbie in TCP/IP programming so there are a few
questions:
- is there a library with handy functions that I can use (Windows
based to start with)?
- does a network connection behave like a file on disk that can may be
read from or written to?
- a pointer to a book that explains these issues (preferably with easy
to understand pictures ;-)?
Hans

Network programming is not standardized and vastly different on Windows
and Linux, so writing a program that will compile and run without
modifications on both is going to be very hard. You're going to have to
use tons of #ifdefs and conditional compiling for the non-portable
parts.

Note that I said "compile and run" there. Writing a program in C that
will run on both Windows and Linux is impossible because Windows uses a
not only different libraries, but also a different format for binaries
than Linux (.exe versus elf, respectively) and the two are not compatible.
(Some smartass will now probably mention Wine and/or Cygwin. Yes I know
about them, but they both provide only an emulation at best.)

You should seriously consider doing this in a scripting language like
Perl which will be far more portable. Java may also be a viable
alternative.


As for a networking library, on Linux there are libnet and sdl-net among
others, but I don't know about their availablility on Windows.
 
B

Ben Bacarisse

Hans Vlems said:
I'm asked to write a program that runs on a Windows or Linux host and
must set up a telnet connection to a network device. This network
device understands very basic commands (one or two letters) and
produces output, depending on the command. The proud owner of this hi-
tech device doesn't want to initiate a session from the command prompt
but rather run a program that issues the same command(-sequence) every
day and collects the output of the remote device.
My first idea was to have the C program build a command script and
start that script with the system() function.
Looks simple, but system() runs its script asynchronously, i.e. the
originating program has no control over the script.
Second idea: connect to the telnet port of the hi-tech device in the C
program. I'm a newbie in TCP/IP programming so there are a few
questions:
- is there a library with handy functions that I can use (Windows
based to start with)?
- does a network connection behave like a file on disk that can may be
read from or written to?
- a pointer to a book that explains these issues (preferably with easy
to understand pictures ;-)?

I'd use a program called "netcat" (abbreviated to nc). There may be a
version for Windows.

I'd switch to expect (as suggested) if some kind of synchronisation
between commands is needed.

Failing that, Python or Perl (Python if you don't know some Perl
already).
 
N

Noob

Angel said:
Note that I said "compile and run" there. Writing a program in C that
will run on both Windows and Linux is impossible because Windows uses a
not only different libraries, but also a different format for binaries
than Linux (.exe versus elf, respectively) and the two are not compatible.

[Waaay OT]

FYI, the binary format used in Windows is called PE (Portable Executable)
and is an extension of the Common Object File Format (COFF) introduced in
Unix SysV.

https://en.wikipedia.org/wiki/Portable_Executable
(Some smartass will now probably mention Wine and/or Cygwin. Yes I know
about them, but they both provide only an emulation at best.)

Given that people run full-blown Windows games thanks to Wine, I'm not
sure why you would be so dismissive.

https://en.wikipedia.org/wiki/Wine_(software)
 
A

Angel

Angel said:
Note that I said "compile and run" there. Writing a program in C that
will run on both Windows and Linux is impossible because Windows uses a
not only different libraries, but also a different format for binaries
than Linux (.exe versus elf, respectively) and the two are not compatible.

[Waaay OT]

FYI, the binary format used in Windows is called PE (Portable Executable)
and is an extension of the Common Object File Format (COFF) introduced in
Unix SysV.

https://en.wikipedia.org/wiki/Portable_Executable

That's interesting background information and I will admit that I didn't
know that. But it's not very relevant to the fact that such files will
not natively run on Linux. (Although, as you correctly pointed out below,
you can get pretty close.)
Given that people run full-blown Windows games thanks to Wine, I'm not
sure why you would be so dismissive.

https://en.wikipedia.org/wiki/Wine_(software)

I am well aware of what Wine can do. If I sounded dismissive I
apologize, that was not what I intended. I have a lot of respect for
the people who created Wine, or any form of emulation/virtualization for
that matter, it's very impressive technology. My comment was meant to
preempt certain smart guys, not to dismiss Wine.


Since we're, as you also correctly observed, way off topic, let's leave
it at that.
 
J

Jorgen Grahn

....

I'd use a program called "netcat" (abbreviated to nc). There may be a
version for Windows.

I'd switch to expect (as suggested) if some kind of synchronisation
between commands is needed.

Failing that, Python or Perl (Python if you don't know some Perl
already).

I agree with all of that, but a few more points:

- Netcat doesn't really do the telnet protocol with all its arcane
options which you can /almost/ ignore[1]. (Unless the -t option
I see in my nc manual -- OpenBSD version -- is enough.)

- /is/ it really telnet, anyway? Telnet is so old it predates TCP/IP,
and it hasn't been used much for the last decades. Check that
you haven't been given incorrect information, and it's just a plain text
protocol over TCP. The two are often confused, since you can
talk to such a server using many telnet clients (if you don't have netcat
handy).

- Does it have to run on both Windows and Linux? It seems like one of
those requirements people like to place, even though they really
will use just one of the environments. Portable code is good and
everything, but is he willing to pay double price for that?

/Jorgen

[1] According to my limited understanding. It has been in use for
43 years, after all.
 
A

ais523

Jorgen said:
I agree with all of that, but a few more points:

- Netcat doesn't really do the telnet protocol with all its arcane
options which you can /almost/ ignore[1]. (Unless the -t option
I see in my nc manual -- OpenBSD version -- is enough.)
The telnet protocol has lots of options, and gets more every now and
then, but it's designed to fall back to the lowest common denominator;
each side has to talk down to the other side's level. What -t does is
just claim, if requested, that it doesn't support any options.

This can lead to quite a problem with more modern telnet-based programs,
though. If you're trying to access a screen oriented program and tell it
that you can't turn off local echo and can't turn on full duplex (and
-t actually does this!), it's likely to at least raise a few eyebrows,
if not outright tell you that your terminal is unsupported and refuse
to communicate with you.
- /is/ it really telnet, anyway? Telnet is so old it predates TCP/IP,
and it hasn't been used much for the last decades. Check that
you haven't been given incorrect information, and it's just a plain text
protocol over TCP. The two are often confused, since you can
talk to such a server using many telnet clients (if you don't have netcat
handy).

Telnet is still used for some things. (I write C code and I also use
telnet interfaces, in my case to allow public access to certain features
of a program, but the telnet interface isn't written by me in C – I use
the command-line tool for the C program, although I wrote a telnet
implementation in Java a while back that supported at least enough
options for most telnet servers to at least consider talking to it.)

But it does seem quite likely to be TCP rather than telnet specifically.
(Telnet clients and servers will try to negotiate options, but apart
from the options, they'll pass bytes through apart from doubling 0xFF
bytes.)

To the OP: A simple implementation of the telnet protocol is pretty easy
to write in C, by the way. The networking code is a lot harder to
write, and in fact can't be done in any reasonably portable way in C
(and as such is off-topic here); I'd recommend looking into languages
more suitable for the task.
 
L

Les Cargill

Hans said:
I'm asked to write a program that runs on a Windows or Linux host and
must set up a telnet connection to a network device. This network
device understands very basic commands (one or two letters) and
produces output, depending on the command. The proud owner of this hi-
tech device doesn't want to initiate a session from the command prompt
but rather run a program that issues the same command(-sequence) every
day and collects the output of the remote device.
My first idea was to have the C program build a command script and
start that script with the system() function.
Looks simple, but system() runs its script asynchronously, i.e. the
originating program has no control over the script.
Second idea: connect to the telnet port of the hi-tech device in the C
program. I'm a newbie in TCP/IP programming so there are a few
questions:
- is there a library with handy functions that I can use (Windows
based to start with)?
- does a network connection behave like a file on disk that can may be
read from or written to?
- a pointer to a book that explains these issues (preferably with easy
to understand pictures ;-)?
Hans

it is entirely possible that you can just open a TCP/IP socket against
port 23 and treat it like a file handle.
 
H

Hans Vlems

it is entirely possible that you can just open a TCP/IP socket against
port 23 and treat it like a file handle.

This is what I was looking for: the magic word is "socket". Now Google
is my friend....
I've looked at Python, but at this point there is no time to learn
another programming language.
It is not a commercial project, a friend asked for a favor. Basically
no problem if it is a routine
program; right now the few empty brain cells left are used for
aviation theory ;-)
At this point the socket approach seems the most promising way.
Thanks all for responding,
Hans
Hans
 
H

Hans Vlems

This sounds like a job for expect, which runs on multiple platforms. You can
also do this in plain Tcl and other scripting languages which offer a uniform
network interface for a variety of platforms beyond unices and Windows.

For Tcl and expect help you can try comp.lang.tcl.

--
My name Indigo Montoya.  \\        Annoying Usenet one post at a time.
You flamed my father.     \'         At least I can stay in character.
Prepare to be spanked.   //               When you look into the void,
Stop posting that!      `/  the void looks into you, and fulfillsyou.

Given what others wrote about portability of a program I decided to
stick to
Windows and try the socket approach. If it can't be done easily in C
then the
person who asked for help is just as qualified to learn Python or Tcl
as I am.
And has plenty more time too ;-)
Hans
 

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

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top