1

I want to create a program in C that displays the router password using telnet protocol. The instruction to do that from cmd is:

  • open CMD
  • Type telnet <router ip>
  • Type the router dashboard User
  • Type the router dashboard password
  • Type wireless default

I try with

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

int main(int argc, char *argv[])
{
    system("telnet 192.168.1.1");
    system("Menara");
    system("Menara");
    system("wireless default");
    system("PAUSE");    
    return 0;
}

but the program stops at the second argument so I assume that system() cannot communicate with telnet. Can someone give me the correct code?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mehdi
  • 41
  • 9
  • Maybe [`popen`](http://linux.die.net/man/3/popen) helps. – cadaniluk Dec 27 '15 at 22:50
  • 2
    Every time you call `system` it runs a new, independent, system command. It dose not type stuff to a shell ( If these is a procedure to do this you would have to tell it which shell ). – ctrl-alt-delor Dec 27 '15 at 23:03
  • 2
    you say that the router to display the password, but then go on to say that you tell the router the password. Is this correct?, or am I missing something? – ctrl-alt-delor Dec 27 '15 at 23:06
  • 2
    You would probably be much better off actually connecting directly using sockets rather than trying to run telnet from your program... – jcaron Dec 28 '15 at 00:39

1 Answers1

2

You need to open a pipe to the telnet command and send commands to it. Look at the popen() function in C and look at this StackOverflow question.

Here is a code example (taken from the StackOverflow question that I just pointed before):

#include <stdio.h>

int main()
{
   FILE *fp = popen("telnet 192.168.1.1","w");

   fprintf(fp, "Menara\n");
   fprintf(fp, "Menara\n");
   fprintf(fp, "PAUSE\n");

   if (pclose(fp) != 0) {
       /* Error reported by pclose() */
       fprintf (stderr, "Could not run more or other error.\n");
   }

   return 0;
}
Community
  • 1
  • 1
perror
  • 7,071
  • 16
  • 58
  • 85
  • Do you need to send `wireless default` too? In general terms, this will work, displaying the output from `telnet` on the command window. It will not allow the program to capture the output from `telnet`; for that, you'd need two pipes, one from the program to `telnet` and another from `telnet` to the program. That's considerably more fiddly to set up — not impossible, but fiddlier. – Jonathan Leffler Dec 28 '15 at 00:31
  • Ok, added (to follow exactly what is written in the original question). – perror Dec 28 '15 at 00:44
  • Can you tell me how to creat the second pipe? @JonathanLeffler – Mehdi Dec 28 '15 at 22:46
  • @Mehdi: You need to use the `pipe()` call twice, then `fork()`. The child will do some plumbing (with `dup2()`) so that one pipe is its standard input and the other its standard output (and the original descriptors returned by `pipe()` are all closed), before executing `telnet` with the appropriate arguments. The parent closes the ends of the pipes that it won't use, writes its data to the write end of the child's standard input, and reads the responses from the read end of the child's standard output. You might need to read first, then write responses. Remember, `popen()` is unidirectional. – Jonathan Leffler Dec 28 '15 at 22:53
  • @Mehdi: All that assumes a sufficiently POSIX-like environment. You mention CMD in the question which leaves me wondering if you're on Windows. If you can use Cygwin or MinGW, you may find it easier, but I'm not as familiar with what Windows provides you in lieu of `pipe()` and `fork()`, etc. It is often worth mentioning the platform, either in the text of the question or in the tags (or both); it directs those answering into the more helpful direction. – Jonathan Leffler Dec 28 '15 at 22:56
  • @JonathanLeffler I'm working on windows xp and i cannot use pipe(); function. Have you another way?. – Mehdi Dec 30 '15 at 13:08
  • @Mehdi: It is basic manual bashing work. I expect that there are mechanisms in XP analogous to pipes bur you can search SO and MSDN just as well as I can — and I would have to search to get the answer. – Jonathan Leffler Dec 30 '15 at 14:37