How to program a telnet client?


 
Thread Tools Search this Thread
Top Forums Programming How to program a telnet client?
# 1  
Old 01-04-2002
How to program a telnet client?

Hi, Experts:
I have programmed a simple telnet client in sco unix 5.0.5, the client has passed throught the initial option negotiation, but I can't receive login prompt from the server. please help me.
# 2  
Old 01-08-2002
# 3  
Old 01-16-2002
thank you, thalex, but I haven't solved the problem by now.

Last edited by thinker130; 01-16-2002 at 04:44 AM..
# 4  
Old 01-16-2002
the source file (the first half)

I post the source files as belows, which include tel_cli.c and tel_cli.h. Can anyone tell me how can I get login prompt and password prompt from the remote host?

tel_cli.c:
// Name: tel_cli.c
// Author: Robert
// Date: Jan 03 2002--- ?
// Called: This module is a part of <<Traffic Data Management>> application.
// Purpose: Used to login into the remote machine and run command on the remote machine.
// Format: tel_cli $1
// Input: $1 is the hostname of the remote machine, config in the local /etc/hosts file.
// Example: tel_cli 134.132.5.11 or tel_cli lyts1
// return: none


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <termio.h>
#include <unistd.h>
#include <stdarg.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <stdarg.h>
#include "tel_cli.h"

#define LINELEN 256


// the struction include option id, option name and option flag, the flag indicates
// if the client answer to the option yes or no, flag ==1 means that yes.
struct OptionFlag
{
int Id;
char * Name;
int Flag;
};
struct OptionFlag struOptionFlag[40];


extern int errno;

int create_server(int port);
int create_ftp_cli(const char *host, const char *service);
int get_reply(int sock);
int get_ts(int sock);
int connectTCP(const char *host, const char *service);
int errexit(const char * format,...);

void sigroutine(int unused);

int ser_sock; //the server socket waiting for the ftp server to connect and accept them a data_sock.
int data_sock = 0; //the socket for receiving or sending data file.
int ctrl_sock;

int para[7];
char * gs_filename; //file name we need
char * gs_omc_passwd;
int g_iFileLen; //the length of file
int g_iReceivedLen = 0; //the bytes has been received

unsigned char gsSendMsg[LINELEN];
unsigned char gsReceiveMsg[LINELEN];


fd_set all_fds;


// main progress, according to the number of parameters, the application will decide what to do.
int main(int argc,char * argv[])
{
char *host = "lyts1"; // host to use if none supplied
char *service = "telnet"; // default service name

switch (argc)
{
case 1:

printf("Usage: getfile [front-name yymmdd]\n");
exit(1);

case 3:
//service = argv[2];
printf("Usage: getfile [front-name yymmdd]\n");

case 2:
host = argv[1];
//gs_omc_passwd = argv[2];
//gs_filename = argv[3];
break;

default:
fprintf(stderr,"Usage: getfile [front-name yymmdd]\n");
exit(1);
}

FD_ZERO(&all_fds);
ctrl_sock = create_telnet_cli(host, service);
FD_SET(ctrl_sock, &all_fds);
printf("ctrl_sock:%d\n", ctrl_sock);

//send_command(ctrl_sock);
exit(0);
}


// Designer: Robert
// Date: Jan 03 2002--- ?
// Called: This module is called by main process.
// Purpose: Used to create a socket connected to the specified host, then communicate with the host to have the options,
// send login name and password to the host, return sock_id.
// Input: $1(char * host): is the host name of the remote machine.
// $2(char * service): is the service type provided by the remote machine, config in the local /etc/services file.
// return: li_sock
int create_telnet_cli(const char *host, const char *service)
{
unsigned char rbuf[LINELEN]; // buffer for one line of text
unsigned char wbuf[LINELEN]; // buffer for one line of text
unsigned char msg[LINELEN];
unsigned char ch;
unsigned char lcOption;
int li_sock, li_count; // li_socket descriptor, read count
int outchars, inchars; // characters sent and received
int i;

printf("entering into telnet_cli\n");
li_sock = connectTCP(host,service);
printf("li_socket is: %d\n", li_sock);
bzero(wbuf, LINELEN);
bzero(rbuf, LINELEN);


sprintf(wbuf, "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", IAC, DODO, 3, IAC, WILL, 24,IAC, WILL, 31,\
IAC, WILL, 32, IAC, WILL, 33, IAC, WILL, 34, IAC, WILL, 39, IAC, DODO, 5);
write(li_sock, wbuf, strlen(wbuf));

//sprintf(wbuf, "%c%c%c\n", IAC, WONT, 37);
//write(li_sock, wbuf, strlen(wbuf));

while ((li_count = recv(li_sock, rbuf, LINELEN, 0))>=0 )
{
printf("length: %d, %s\n", li_count, rbuf);
//sleep(5);
if (li_count == 0)
{
printf("socket has been closed!\n");
exit(1);
}
//if li_count > 0, it means that the socket has received some characters from the server
for (i = 0; i <= li_count - 1; )
{
printf("rbuf[%d] is : %d\n", i, rbuf[i]);
//sleep(2);
//ch = rbuf[i];
//printf("%c\n",ch);
if ( rbuf[i] == IAC)
{
//printf("rbuf[%d] is IAC\n", i);
//gsSendMsg[i]= rbuf[i];

ch = rbuf[i + 1];
printf("%d\n", ch);
//i++;
lcOption = rbuf[i + 2];
printf("fdsafafsafasfsafsa%d\n", lcOption);
i = i + 3;
switch (ch)
{
case DODO:
if (lcOption == AUTH)
{
printf("RECV DO AUTHENTICATION (37)\n");
//sleep(5);
bzero(gsSendMsg, LINELEN);
sprintf(gsSendMsg, "%c%c%c\n", IAC, WONT, AUTH);
write(li_sock, gsSendMsg, strlen(gsSendMsg));
}

if (lcOption == NAWS)
{
printf("RECV DO NAWS (31)\n");
//sleep(5);
bzero(gsSendMsg, LINELEN);
//////SEND IAC SB NAWS 0 132 (132) 0 52 (52)
//the key point is to find SB syntax.
sprintf(gsSendMsg, "%c%c%c%d%d%d%d%c%c\n", IAC, SB, NAWS, 0,80,0,24,IAC,SE);
write(li_sock, gsSendMsg, strlen(gsSendMsg));
}

if (lcOption == XDISPLOC)
{
printf("RECV DO XDISPLOC (35)\n");
//sleep(5);
bzero(gsSendMsg, LINELEN);
sprintf(gsSendMsg, "%c%c%c\n", IAC, WONT, XDISPLOC);
write(li_sock, gsSendMsg, strlen(gsSendMsg));
}

if (lcOption == OLD_ENVIRON)
{
printf("RECV DO OLD-ENVIRON (36)\n");
//sleep(5);
bzero(gsSendMsg, LINELEN);
sprintf(gsSendMsg, "%c%c%c\n", IAC, WONT, OLD_ENVIRON);
write(li_sock, gsSendMsg, strlen(gsSendMsg));
}
if (lcOption == ECHO_ON)
{
printf("RECV DO ECHO (1)\n");
sleep(5);
bzero(gsSendMsg, LINELEN);
sprintf(gsSendMsg, "%c%c%c\n", IAC, WONT, ECHO_ON);
write(li_sock, gsSendMsg, strlen(gsSendMsg));
}
break;

case DONT:
printf("ch is DONT\n");
break;
case WILL:
if (lcOption == ECHO_ON)
{
printf("RECV WILL ECHO (1)\n");
sleep(5);
bzero(gsSendMsg, LINELEN);
sprintf(gsSendMsg, "%c%c%c\n", IAC, DODO, ECHO_ON);
write(li_sock, gsSendMsg, strlen(gsSendMsg));
}
break;
# 5  
Old 01-16-2002
the source file(the second half tel_cli.c and tel_cli.h)

case WONT:
break;
case SB:
printf("ch is SB\n");
sleep(5);
if (lcOption == TSPEED)
{
printf("RECV IAC SB TERMINAL SPEED SEND (32)\n");
printf("%d%d%d\n", rbuf[i], rbuf[i + 1], rbuf[i + 2]);
//i = i + 3;
sleep(5);
bzero(gsSendMsg, LINELEN);
sprintf(gsSendMsg, "%c%c%c%c%d%d%d%d%c%c\n", IAC, SB, TSPEED, IS, 38400 >> 8, (38400 & 0xFF), 38400 >> 8, (38400 & 0xFF), IAC,SE);
write(li_sock, gsSendMsg, strlen(gsSendMsg));
}
if (lcOption == NEW_ENVIRON)
{
printf("RECV IAC SB NEW-ENVIRON SEND (39)\n");
printf("%d%d%d\n", rbuf[i], rbuf[i + 1], rbuf[i + 2]);
//i = i + 3;
sleep(5);
bzero(gsSendMsg, LINELEN);
sprintf(gsSendMsg, "%c%c%c%c%c%c\n", IAC, SB, NEW_ENVIRON, IS, IAC, SE);
write(li_sock, gsSendMsg, strlen(gsSendMsg));
}

if (lcOption == TERMINAL)
{
printf("RECV IAC SB TERMINAL TYPE SEND (24)\n");
printf("%d%d%d\n", rbuf[i], rbuf[i + 1], rbuf[i + 2]);
//i = i + 3;
sleep(5);
bzero(gsSendMsg, LINELEN);
sprintf(gsSendMsg, "%c%c%c%c%s%c%c\n", IAC, SB, NEW_ENVIRON, IS,"ANSI", IAC, SE);
write(li_sock, gsSendMsg, strlen(gsSendMsg));
printf("okokok\n");
bzero(gsSendMsg, LINELEN);
sprintf(gsSendMsg, "%c%c%c\n", IAC, DODO,ECHO_ON);
write(li_sock, gsSendMsg, strlen(gsSendMsg));
printf("okokok\n");
}
i = i + 3;
break;

default:
printf("ch is other character\n");
break;
}

}
else
{
//printf("ch isn't IAC, there are maybe some errors occur!\n");
i++;
}
}
}
}



tel_cli.h:

//telnet command code
#define IAC 255
#define DODO 253
#define DONT 254
#define WILL 251
#define WONT 252
#define SB 250
#define SE 240
#define IS '0'
#define SEND '1'
#define INFO '2'
#define VAR '0'
#define VALUE '1'
#define ESC '2'
#define USERVAR '3'

//option name and id
#define ECHO_ON 1
#define GO_AHEAD 3
#define STATUS 5
#define TIMER 6
#define TERMINAL 24
#define NAWS 31
#define TSPEED 32
#define FLOW 33
#define LINEMODE 34
#define XDISPLOC 35 // X Display Location
#define OLD_ENVIRON 36 // Old - Environment variables
#define AUTH 37
#define NEW_ENVIRON 39
# 6  
Old 01-16-2002
the source files(the third part)

I have forgotten to include some functions needed in file tel_cli.c, please append the belowed source code to the end of file tel_cli.c.


/*connectsock - allocate & connect a socket using TCP or TCP*/
int connectsock(const char *host,const char *service, const char *transport)
/* arguments:
* host --- name of host to which connection is desired
* service -- service associated with the desired port
* transport --- name of transport protocol to use ("tcp" or "udp")
*/
{
struct hostent *phe; /* pointer to host information entry */
struct servent *pse; /* pointer to service information entry */
struct protoent *ppe; /* pointer to protocol information entry */
struct sockaddr_in sin; /* an Internet endpoint addredd */
int s,type; /* socket descriptor and socket type */

memset(&sin,0,sizeof(sin));
sin.sin_family=AF_INET;

/* Map service name to port number */
if (pse = getservbyname(service,transport))
sin.sin_port = pse->s_port;
else if ((sin.sin_port = htons((u_short)atoi(service))) == 0 )
{
//errexit("can't get \"%s\" service entry\n",service);
printf("can't get \"%s\" service entry\n",service);
return 0;
}

/* Map host name to IP address,allowing for dotted decimal */
if (phe = gethostbyname(host))
memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
{
//errexit("can't get \"%s\" protocol entry\n",transport);
printf("can't get \"%s\" protocol entry\n",transport);
return 0;
}

/* Map transport protocol name to protocol number */
if ((ppe = getprotobyname(transport))==0)
{
//errexit("can't get \"%s\" protocol entry\n",transport);
printf("can't get \"%s\" protocol entry\n",transport);
return 0;
}

/* User protocol to choose a socket type */
if (strcmp(transport,"udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;

/*Allocate a socket */
s = socket(PF_INET, type, ppe->p_proto);
if (s < 0)
{
//errexit("can't create socket:%s\n", strerror(errno));
printf("can't create socket:%s\n", strerror(errno));
return 0;
}
/* connect the socket */
if (connect (s,(struct sockaddr *)&sin,sizeof(sin)) < 0)
{
//errexit("can't connect to %s.%s: %s\n",host,service,strerror(errno));
printf("can't connect to %s.%s: %s\n",host,service,strerror(errno));
return 0;
}
return s;

}


int connectTCP(const char *host, const char *service)
/* arguments:
* host --- name of host to which connection is desired
* service --- service associated with the desired port
*/
{
return connectsock(host,service,"tcp");
}




/* errexit --- print an error mesage and exit */

/*VARARGS1*/

int errexit(const char *format,...)
{
va_list args;
va_start(args,format);
vfprintf(stderr,format,args);
va_end(args);
exit(1);
}
# 7  
Old 01-16-2002
Re: How to program a telnet client?

Quote:
Originally posted by thinker130
Hi, Experts:
I have programmed a simple telnet client in sco unix 5.0.5, the client has passed throught the initial option negotiation, but I can't receive login prompt from the server. please help me.
I don't have the time to debug all that code that you posted, but I did download and try it.

I found that if I set the service to "daytime", it worked! The daytime service has no option negiation at all. That points the finger at the option negotiation. Ftp has subset of telnet's option negotiation, so I tried setting the service to "ftp" and this also failed, but your program produced much less debug output.

I suggest that you set the service to "ftp" and debug the option negotiation on that. Once you can get a login prompt from ftp you can then try telnet.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

telnet client and logs

Hello everyone. I have a script that telnets into a linux workstation and restarts a service, while logging the session to a file. I'm kind of new to linux so when I was using windows I would capture something like windows cli: telnet xxx.xxx.xxx.xxx -f c:/capture.log ... # /sbin/service... (3 Replies)
Discussion started by: Elyas_cr
3 Replies

2. UNIX for Advanced & Expert Users

difference between logging into unix through f-secure ssh client and telnet

hi, what is the difference between logging into unix through f-secure ssh client and telnet is there any more security check is involved can any one explain thanks in advance (1 Reply)
Discussion started by: trichyselva
1 Replies

3. Programming

SFTP client program

can u help me? i need the program code in C to perform Simple File Transfer in linux.in this forum i found the server program,inw i need the client program ASAP. Thanx. (1 Reply)
Discussion started by: harshi
1 Replies

4. Programming

Server client program

hi guys, I need the code for a server client registration form.The server must ask for authentication .Then the client would send in data. This is stored in a file .The server sends back a receipt to the client as part of the payment done. plz can some 1 get me the code... (9 Replies)
Discussion started by: pip3r
9 Replies

5. Programming

Client - server program

i came acors this coding when surfin the net.this code works perfectly.but as i am new to this socket programming i need sm coments quoted on it or explanation regarding this source code. i have prb understanding the server.c i have posted it below can u guys help me !!!! cheerZ The... (4 Replies)
Discussion started by: mathu
4 Replies

6. UNIX for Dummies Questions & Answers

how to launch program though telnet

I have launched telnet on nt and have connected to a unix server, I'm trying to run a program on the unix server which will launch a gui, but when I try to launch it I get the message "display not set\n" "By default set it to 0.0" I can sit down at the unix machine and launch the program with... (3 Replies)
Discussion started by: cbachman
3 Replies

7. UNIX for Dummies Questions & Answers

IP address of telnet client

Hi, I am using the telnet client on windowsNT to access the Unix system. I want to find out the IP address of the telnet client from the server side. The reason is I want to set the DISPLAY environment variable when the user is logged on. Is there anyway that the the Unix server can... (1 Reply)
Discussion started by: vtran4270
1 Replies

8. Programming

Does Telnet client library exist ?

Hello, I'm new user in this forum, and i'm french ... so excuse me for my english :) In fact, I've to manage several network equipments which can be only configured with the telnet protocol. But using the standalone telnet tool, it's too long and hard. So I'd like to known if a Telnet... (0 Replies)
Discussion started by: aho
0 Replies

9. Programming

Telnet client IP determination

I have configured my firewall to allow only five remote IP's to connect to my server. Upon connection...i would like to automate the Xsession functions for authorized IP's. Mainly, $DISPLAY of the environment to the client. I understand that the "gethostbyaddr" function is capable of this.... (0 Replies)
Discussion started by: thomas.jones
0 Replies

10. UNIX for Dummies Questions & Answers

Using telnet client from MacOSX's command line terminal

I'm completely new to Unix, but familiar with Mac OSX. I've just discovered the command line terminal feature of this new OS and I'm trying to learn how to telnet into my host's server to change permissions to allow executable cgi scripts for my website. Is there anyone who might be able to... (2 Replies)
Discussion started by: tylerl
2 Replies
Login or Register to Ask a Question