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 2 3041
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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,...
|
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...
|
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...
|
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)...
|
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...
|
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);...
|
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....
|
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 =...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |