Search code examples
csocketstelnet

Debug a Telnet connection in C


I wrote a simple C function which is connect to a switch (by using Telnet socket), and there run various cli-commands and exit. I want to use with a Cisco and a Edge-Core switch.

With Edge-Core switch this function work fine, but does not work at Cisco, because it hang in the "login" process.

Here is the mentioned C function:

#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>

char *telnet_switch( char *switch_ip, char *cmd, char cmd_out[] ) {
  char                  buf[5000];
  int                   sock;
  int                   bytes;
  struct sockaddr_in    sw;

  sock = socket( PF_INET, SOCK_STREAM, 0 );

  sw.sin_family      = AF_INET;
  sw.sin_port        = htons( 23 );
  sw.sin_addr.s_addr = inet_addr( switch_ip );

  if ( sock < 0 ) {
    perror("Socket creation error!");
    return (cmd_out);
  }

  if ( connect( sock, (struct sockaddr *) &sw, sizeof( sw ) ) < 0 ) {
    perror("Connect process error!");
    return (cmd_out);
  }

  send( sock, "admin\n\r", 7, 0 );
  usleep(250000);
  bytes = recv( sock, buf, sizeof( buf ), 0 );

  printf("Bytes: %d\n", bytes);  // DEBUG MESSAGE 1.

  send( sock, "test\n\r", 6, 0 );
  usleep(250000);
  bytes = recv( sock, buf, sizeof( buf ), 0 );

  printf("Bytes: %d\n", bytes);  // DEBUG MESSAGE 2.

  send( sock, cmd, strlen( cmd ), 0 );
  send( sock, "\n\r", 2, 0 );

  memset( buf, 0, sizeof( buf ) );
  memset( cmd_out, 0, sizeof( cmd_out ) );

  usleep(250000);
  recv( sock, buf, sizeof( buf ), 0 );

  sprintf( cmd_out, buf );

  close( sock );
  return (cmd_out);
}

And the main (test) function:

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

char *telnet_switch( char *, char *, char [] );

int main( int argc, char **argv ) {
  char cmd_out[1000000];

  telnet_switch("192.168.16.18", "?", cmd_out);
  printf("%s\n", cmd_out);

  exit (0);
}

The output at the Edge-Core switch:

szpal@testnms:~/tmp$ ./test
Bytes: 35
Bytes: 110
edge-core:/>
General Commands:
-----------------
Help/?: Get help on a group or a specific command
Up    : Move one command level up
Logout: Exit CLI

Command Groups:
---------------
System      : System settings and reset options
IP          : IP configuration and Ping
Port        : Port management
MAC         : MAC address table
VLAN        : Virtual LAN
...
VCL         : VLAN Control List
Debug       : Switch debug facilities

Type '<group>' to enter command group, e.g. 'port'.
Type '<group> ?' to get list of group commands, e.g. 'port ?'.
Type '<command> ?' to get help on a command, e.g. 'port mode ?'.
Commands may be abbreviated, e.g. 'por co' instead of 'port configuration'.
edge-core:/>
Nyomjon meg egy billentyűt...

And the output at the Cisco switch:

szpal@testnms:~/tmp$ ./test
Bytes: 12

So i dont know why it cannot login to the Cisco switch, i tried to increase the 'usleep' timeouts, i tried the '\r\n' chars but no success.

There is a Debug process to solve this problem?


Solution

  • When I need to troubleshoot ethernet connection problems, I usually turn to Wireshark. I think that if you compare the connection process of your code versus a standard telnet client using Wireshark, you will probably be able to spot what you are doing wrong.