Problem with Telnet via popen() on an embedded system

Hi there,

I’m just having a problem to use the telnet command via popen().

If I use the attached code to telnet to a fully installed QNX 6.3.0 SP 3 there is no problem (172.16.205.6).
But if I try to use the same code to telnet to an embedded system running with QNX 6.5.0 it won’t ask for username/password (172.16.205.7). :frowning:

If I use the telnet program directly from shell I can telnet to BOTH systems. :confused:

What is missing on the embedded system or what do I have do account for?

Kind regards

Martin


./myTelnet_g 172.16.205.6

telnet> Trying 172.16.205.6…
Connected to 172.16.205.6.
Escape character is ‘^]’.
login: 1
2
root
Password:
Tue Mar 15 12:09:48 2011 on /dev/ttyp2
Last login: Tue Mar 15 12:08:17 2011 on /dev/ttyp2
edit the file .profile if you want to change your environment.
To start the Photon windowing environment, type “ph”.

ls -l

total 194
drwx------ 5 root root 4096 Jan 11 09:23 .
drwxr-xr-x 18 root root 4096 Feb 04 10:09 …
-rw-rw-r-- 1 root root 51 Mar 15 12:09 .lastlogin
drwx------ 3 root root 4096 Jul 05 2007 .mozilla
drwxrwxr-x 10 root root 4096 Aug 30 2010 .ph
-rw-r–r-- 1 root root 191 May 07 2004 .profile
drwxrwxr-x 7 root root 4096 Aug 19 2010 ide4-workspace
-rw-rw-rw- 1 root root 77563 Aug 07 2007 module.code

exit

Connection closed by foreign host.


./myTelnet_g 172.16.205.7

telnet> Trying 172.16.205.7…
Connected to 172.16.205.7.
Escape character is ‘^]’.

QNX Neutrino () (ttyp0)

Connection closed by foreign host.
1
2


#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
char achTelnetString[255+1];
FILE *fhTelnet = NULL;

sprintf(achTelnetString, “telnet”);
fhTelnet = popen(achTelnetString, “w”);
if(fhTelnet != NULL)
{
fprintf(fhTelnet, “open %s\n”, argv[1]);
fflush(fhTelnet);
sleep(2);
printf(“1\n”); // Just to see
sleep(2);
printf(“2\n”); // what’s going on

fprintf(fhTelnet, "root\n");
fflush(fhTelnet);

fprintf(fhTelnet, "mypassword\n");
fflush(fhTelnet);

fprintf(fhTelnet, "ls -l\n");
fflush(fhTelnet);
sleep(1);
fprintf(fhTelnet, "exit\n");
fflush(fhTelnet);
sleep(1);

pclose(fhTelnet);
fhTelnet = NULL;

}

return(0);
}

I’m not sure if the problem is something in QNX 6.5.0 or something missing from the embedded system. I have a system I can test on later today.

However I would suggest an alternative. Telnet is a relatively straight forward protocol. You open a TCP/IP connection and then there is a little bit of negotiation to deal with different capabilities on each side. You will have a much more robust program if you do it at that way. I did something like this in the distant past, and though I don’t know where to find it, I recall that it wasn’t a big deal.

You might also try Curl which is provided on 6.5.0.

I can confirm that the program works on 6.5.0, so you are correct, something is missing from your embedded system. Usually this is a shared library, however when that happens you usually get a clear message on the console as to what is missing.

But unfortunately there is no more output than what I showed in my posting, no additional error message or whatever. If I had a clear message or even at least a message I would have copied it to my posting.

As mentioned:

“If I use the telnet program directly from shell I can telnet to BOTH systems.”

So there is no “normal problem” to use telnet, because I can connect/use telnet from the shell to both systems, the full system and the embedded system. But I need to telnet via c code to automate some things.

Martin

Telnet is a very simple protocol. You should be able to find C code on the web and use that code instead of calling telnet via popen which is always a touchy operation.