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;