![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| i want a UDP Client receiving program | Nirmala | IP Networking | 1 | 06-10-2005 03:46 PM |
| IP address of telnet client | vtran4270 | UNIX for Dummies Questions & Answers | 1 | 09-12-2002 04:34 PM |
| Does Telnet client library exist ? | aho | High Level Programming | 0 | 09-11-2002 08:45 AM |
| Telnet client IP determination | thomas.jones | High Level Programming | 0 | 08-08-2002 06:17 PM |
| Using telnet client from MacOSX's command line terminal | tylerl | UNIX for Dummies Questions & Answers | 2 | 07-15-2001 10:05 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
||||
|
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. |
|
||||
|
|
|
||||
|
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; |
|
||||
|
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 |
|
||||
|
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); } |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|