473,231 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

telnet

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

Nov 14 '05 #1
2 3032
On 2004-11-15, newsf <ne***@grupalia.com> wrote:
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

--
Christian Staudenmayer
University of Ulm, Germany
cs****@gmail.com
Nov 14 '05 #2
newsf <ne***@grupalia.com> spoke thus:
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

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dan | last post by:
I'm writing a simplistic telnet client in VB6 and I've run into a small snag. The program has a textbox to write in the string to be sent using ..SendData and has another textbox that displays...
3
by: Yannick Turgeon | last post by:
Hello all, I'm currently trying to pass commands to a telnet session and get the texte generated (stdin + stdout) by the session. The problem I get is that the Telnet.read_until() function...
4
by: Donnal Walter | last post by:
On Windows XP I am able to connect to a remote telnet server from the command prompt using: telnet nnn.nnn.nnn.nnn 23 where nnn.nnn.nnn.nnn is the IP address of the host. But using telnetlib,...
2
by: john brown | last post by:
I'm telnetting into a router. Apart from the fact I can't seem to view the output when iniciating the session, I can't match one of the expressions using Net::Telnet. I can telnet into the router...
0
by: CJ | last post by:
Can someone look at the code below and tell me whats going on? Here is the problem. I can successfully telnet one of our routers and successfully put in a command in the while statement, but...
2
by: eight02645999 | last post by:
hi i am using a telnet session to simulate an authentication mechanism USER = "user" PASSWORD = "password" try: telnet = telnetlib.Telnet(HOST) telnet.set_debuglevel(5)...
2
by: thilandeneth | last post by:
i need to do telnet via a web server please give me a idia to initiate the project following requirements are needed 1 Create web based custom telnet client to communicate with remote...
1
by: perlxyo999 | last post by:
Hi , I have a problem doing manipulation (like split) in string comeing back from telnet commnd : $HOST = 'xxx'; $USER = 'yyy20'; $PASS = 'yyy-pass'; #$telnet = Net::Telnet->new($HOST);...
6
by: sherrygomindes | last post by:
Hi I have written a perl script using the Telnet module. I need to remotely login in from one windows XP machine to another XP machine. But i get errors which i can't figure out the reason....
17
by: ravimath | last post by:
Dear all, I have written following script to loin to router bu it is showing error. #!c:\Perl\bin; use strict; use warnings; my $hostname = 'REMOVED FOR YOUR PROTECTION'; my $password =...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.