Sponsored Content
Top Forums Programming How to program a telnet client? Post 13359 by thinker130 on Wednesday 16th of January 2002 03:06:05 AM
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;
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 04:17 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy