title: telnet client using c, for automating the backup of a series of routers
body: <p>I have 400 routers, approx. and by using the console, I can access by telnet each one of them, and by typing a series of commands I can generate a backup and copy it to my computer using FTP.</p>
  
  <p>I want to automate this task because its really annoying.</p>
  
  <p>I have read everything I could about this topic on the Internet.</p>
  
  <p>I know to program in C and I am using Windows. So I read all about WINSOCK and sockets and I am trying to implement my program. </p>
  
  <p>I have this problem, when I use the <code>recv</code> function it keeps showing me strange characters, I don't know what this means and how to solve it and I dont even know if what I am trying to do is possible using just sockets and C. </p>
  
  <p>In case it is possible could you tell me how to send the commands I have this way or point me in the way of a document that shows me how to do this. </p>
  
  <p>here goes the basic of my code, i suspect i should learn about how to negotiate telnet in c</p>
  
  <pre><code>#ifndef WIN32_LEAN_AND_MEAN
  #define WIN32_LEAN_AND_MEAN
  #endif
  
  #include &lt;windows.h&gt;
  #include &lt;winsock2.h&gt;
  #include &lt;ws2tcpip.h&gt;
  #include &lt;iphlpapi.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  
  #pragma comment(lib, "Ws2_32.lib")
  #pragma comment(lib, "wsock32.lib")
  #pragma comment(lib, "libws2_32.lib")
  #pragma comment (lib, "Mswsock.lib")
  #pragma comment (lib, "AdvApi32.lib")
  
  
  int main ()
  {
      const char direcciones[400]="xx.x.x.xx";
      WSADATA wsaData;
      SOCKET sock;
      struct hostent *host;
      struct sockaddr_in direc;
      int conex;
      int len;
      char *sendbuf1="USER admin\n\r";
      char *sendbuf2="PASS \n\r";
      char *sendbuf3="\n\r";
      char *sendbuf4="export file=";
  
      char recvbuf[512];
      int cont;
  
      // Initialize Winsock
      WSAStartup(MAKEWORD(2,2), &amp;wsaData);
      conex = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
      if (conex != 0)
      {
      printf("WSAStartup failed: %d\n", conex);
      return 1;
      }
  
      host=gethostbyaddr(&amp;direcciones[0],4,AF_INET);
      printf("\tOfficial address: %s\n", host-&gt;h_addr);
      system("pause");
  
      sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
      if (sock==-1)
      {
          printf("error al crear el socket\n");
          return(-1);
  
      }
      direc.sin_family=AF_INET;
      direc.sin_port=htons(23);
      direc.sin_addr=*((struct in_addr*)host-&gt;h_addr);
      memset(direc.sin_zero,0,8);
  
      conex = connect(sock,(SOCKADDR *) &amp; direc,sizeof(direc));
      if (conex==SOCKET_ERROR)
      {
      printf("no se ha podido conectar con el servidor\n");
      conex = closesocket(sock);
          if (conex == SOCKET_ERROR)
              wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
      WSACleanup();
      return 1;
      }
      else printf("coneccion correcta a puerto telnet\n");
  
  system("pause");                    //esto hay que quitarlo despues
  
  conex = recv(sock,recvbuf,512, 0);
  printf("se recibio %i bytes\n",conex);
  printf("dato recibido: %s\n",&amp;recvbuf);
  
  
  conex = send( sock, sendbuf1,strlen(sendbuf1), 0 );
  
  
  conex = recv(sock,recvbuf,512, 0);
  printf("se recibio %i bytes\n",conex);
  printf("dato recibido: %s\n",&amp;recvbuf);
  
  
  conex = closesocket(sock);
  if(conex==SOCKET_ERROR)
  {
      wprintf(L"funcion closesocket fallo con error: %ld\n", WSAGetLastError());
      WSACleanup();
      return 1;
  }
  
  WSACleanup();
  return 0;
  
  }
  </code></pre>
  
  <p>`</p>