telnet

N

newsf

Hi,
I want to make a program what connect through telnet protocol to a router,
and run commands for configure the router. I read over sockets and I made
the program above, but it don´t work fine.

After this, I read document in Internet about this, and I believe that I
have to implement the protocol. But I don´t know.

Someone can help me.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 23 /* El puerto donde se conectara
*/
#define MAXDATASIZE 100 /* maxima cant. De bytes que se pueden recibir
en una llamada a recv */
int main(int argc, char *argv[])
{
int sockfd, numbytes; /* Contendra el numero de bytes
recibidos despues de llamar a recv() */
int newfd; /* las transferencias de datos se realizar
mediante newfd */
char buf[MAXDATASIZE]; /* Buffer donde se reciben los datos */
struct hostent *he; /* Se utiliza para
convertir el nombre del host a su direccion IP */
struct sockaddr_in their_addr; /* direccion del server donde se
conectara */
/* Tratamiento de la linea de comandos. */
if (argc != 2)
{
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
/* Convertimos el nombre del host a su direccion IP */
if ((he=gethostbyname(argv[1])) == NULL)
{
herror("gethostbyname");
exit(1);
}
/* Creamos el socket */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
/* Establecemos their_addr con la direccion del server */
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), 8);
/* Intentamos conectarnos con el servidor */
if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))
== -1)
{
perror("connect");
exit(1);
}

/* Recibimos los datos del servidor */
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
perror("recv");
exit(1);
}


/*prueba de envio de datos*/
if (send(sockfd, "abcd\n", 18, 0) == -1)
perror("send");
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);

/* Recibimos los datos del servidor */
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
perror("recv");
exit(1);
}
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);

/*prueba de envio de datos*/
if (send(sockfd, "exit\n", 6, 0) == -1)
perror("send");
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);


/* Devolvemos recursos al sistema */
close(sockfd);
return 0;
}

thanks,

Fernando
 
C

Christian Staudenmayer

Hi,
I want to make a program what connect through telnet protocol to a router,
and run commands for configure the router. I read over sockets and I made
the program above, but it don´t work fine.

After this, I read document in Internet about this, and I believe that I
have to implement the protocol. But I don´t know.

Someone can help me.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 23 /* El puerto donde se conectara
*/
#define MAXDATASIZE 100 /* maxima cant. De bytes que se pueden recibir
en una llamada a recv */
int main(int argc, char *argv[])
{
int sockfd, numbytes; /* Contendra el numero de bytes
recibidos despues de llamar a recv() */
int newfd; /* las transferencias de datos se realizar
mediante newfd */
char buf[MAXDATASIZE]; /* Buffer donde se reciben los datos */
struct hostent *he; /* Se utiliza para
convertir el nombre del host a su direccion IP */
struct sockaddr_in their_addr; /* direccion del server donde se
conectara */
/* Tratamiento de la linea de comandos. */
if (argc != 2)
{
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
/* Convertimos el nombre del host a su direccion IP */
if ((he=gethostbyname(argv[1])) == NULL)
{
herror("gethostbyname");
exit(1);
}
/* Creamos el socket */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
/* Establecemos their_addr con la direccion del server */
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), 8);
/* Intentamos conectarnos con el servidor */
if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))
== -1)
{
perror("connect");
exit(1);
}

/* Recibimos los datos del servidor */
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
perror("recv");
exit(1);
}


/*prueba de envio de datos*/
if (send(sockfd, "abcd\n", 18, 0) == -1)
perror("send");
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);

/* Recibimos los datos del servidor */
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
perror("recv");
exit(1);
}
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);

/*prueba de envio de datos*/
if (send(sockfd, "exit\n", 6, 0) == -1)
perror("send");
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);


/* Devolvemos recursos al sistema */
close(sockfd);
return 0;
}

thanks,

Fernando

Have a close look at this:
http://www.ecst.csuchico.edu/~beej/guide/net/html/

Greetings, Chris
 
N

newsf

Ok Chris, I had a client an a server socket. I had intall the server socket
in a PC and run. After I had intall the client socket in another PC. The
result of this is OK. The server send the messages at the client and
viceversa.

After this I had do a client socket to connect to a router, authenticate in
the router and configure this router, but nor achieve authenticate nor
configure the router.

I think that only send messages to router of password and configuration is
not enough. I think that must send it follow some protocol (ej.: first send
escape sequence, later send commands, later send data, etc...).

¿Where I can find information about this?......

Thanks,

Fernando

Christian Staudenmayer said:
Hi,
I want to make a program what connect through telnet protocol to a router,
and run commands for configure the router. I read over sockets and I made
the program above, but it don´t work fine.

After this, I read document in Internet about this, and I believe that I
have to implement the protocol. But I don´t know.

Someone can help me.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 23 /* El puerto donde se conectara
*/
#define MAXDATASIZE 100 /* maxima cant. De bytes que se pueden recibir
en una llamada a recv */
int main(int argc, char *argv[])
{
int sockfd, numbytes; /* Contendra el numero de bytes
recibidos despues de llamar a recv() */
int newfd; /* las transferencias de datos se realizar
mediante newfd */
char buf[MAXDATASIZE]; /* Buffer donde se reciben los datos */
struct hostent *he; /* Se utiliza para
convertir el nombre del host a su direccion IP */
struct sockaddr_in their_addr; /* direccion del server donde se
conectara */
/* Tratamiento de la linea de comandos. */
if (argc != 2)
{
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
/* Convertimos el nombre del host a su direccion IP */
if ((he=gethostbyname(argv[1])) == NULL)
{
herror("gethostbyname");
exit(1);
}
/* Creamos el socket */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
/* Establecemos their_addr con la direccion del server */
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), 8);
/* Intentamos conectarnos con el servidor */
if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))
== -1)
{
perror("connect");
exit(1);
}

/* Recibimos los datos del servidor */
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
perror("recv");
exit(1);
}


/*prueba de envio de datos*/
if (send(sockfd, "abcd\n", 18, 0) == -1)
perror("send");
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);

/* Recibimos los datos del servidor */
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
perror("recv");
exit(1);
}
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);

/*prueba de envio de datos*/
if (send(sockfd, "exit\n", 6, 0) == -1)
perror("send");
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);


/* Devolvemos recursos al sistema */
close(sockfd);
return 0;
}

thanks,

Fernando

Have a close look at this:
http://www.ecst.csuchico.edu/~beej/guide/net/html/

Greetings, Chris
 
C

Christopher Benson-Manica

newsf said:
Ok Chris, I had a client an a server socket. I had intall the server socket
in a PC and run. After I had intall the client socket in another PC. The
result of this is OK. The server send the messages at the client and
viceversa.
?Where I can find information about this?......

1) Please don't top-post.
2) You'll do a lot better on a different newsgroup, possibly
comp.unix.programmer.
3) Please read the FAQ and welcome messages:

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top