로그인
Sign in
/******************************************************************************
*                                                                                                                                                          *
*                                                T E L N E T  C O M M A N D E R                                                     *
*                                                ------------------------------                                              *
*******************************************************************************
*                                                                                                                                                          *
* 시  스  템 : TOOLS                                                                                                                  *
*                                                                                                                                                          *
* 프로그램명 : [telnet_cmd.c]                                                                                                  *
*                                                                                                                                                          *
*           기능 : [cmd 파일에 등록된 명령을 수행시킨다.]                                              *
*                                                                                                                                                          *
*           설명 : [cmd 파일에 등록된 명령을 수행시킨다.]                                              *
*                                                                                                                                                          *
*         작성자 : [배 재영]                                                                                                              *
*                                                                                                                                                          *
*         작성일 : [2005/01/01]                                                                                                      *
*                                                                                                                                                          *
* 작성프로그램 :                                                                                                                          *
*                         Function Prototype을 참조하십시오.                                                              *
*                                                                                                                                                          *
*   아규먼트 : 순서   argv 값                                   설 명                                                  *
*                          -----  ----------------------  -------------------------------  *
*                          [1]        구분자                                         MSC(.netrc에 등록된 머신유형)           *
*                          [2]        시스템번호                                  시스템 번호(01)                                   *
*                                                                                                                                                          *
*   Argument : telnet_cmd                                                                                                          *
*                                                                                                                                                          *
*         리턴값 :  0 : Success                                                                                                             *
*                           1 : Failure                                                                                                             *
*                                                                                                                                                          *
* Compile Option :                                                                                                                          *
*          @make telnet_cmd                                                                                                                  *
*                                                                                                                                                          *
* Special Logic : 구분자+시스템번호가 .netrc에 등록 된 머신임.                                  *
*******************************************************************************
*   수정이력 :                                                                                                                                  *
*                                                                                                                                                          *
*   날   짜         성  명         함  수  명          수정 내용                                                                            *
*   ----------  ---------  --------------  ---------------------------------- *
*   2005/01/01  배재영         초기작성(기존 프로그램을 재분석하여 새로 개발함)     *
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <regex.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>

/*
** Defines Constants
*/
#define U_DEBUG   /* U_DEBUG ON  */
#undef  U_DEBUG   /* U_DEBUG OFF */

#define HOME         getenv("HOME")
#define MAX3         (3)
#define MAX48         (48)
#define MAXLINE  (BUFSIZ)
#define SUCCESS  (0)
#define FAILURE  (-1)

#define TAG_MSC  ("MSC")
#define NETRC          (".netrc")

#define SYSERRLOG ("%s/log/TELNET.%.4s-%.2s-%.2s")

#define SECOND(h, m, s) ( h*60*60 + m*60 + s )

#define SYSERR(f,eno) \
        fprintf(stderr, "Error %s(%d) %s\n", f, eno, strerror(eno));\
        fflush(stderr);

#define CMD_FILE  ("%s.cmd")

/*
** Defines structure
*/
typedef struct info
{
        char   *argv[2];                /* 임력 argv값                    */
        char   r_host[16];                /* 머신 명                            */
        char   userid[16];                /* LOGIN ID                            */
        char   passwd[16];            /* 계정 암호                         */
        char   cmds[BUFSIZ];        /* COMMAND들                        */
        time_t systime;                    /* 시스템 date,time         */
} INFO_T;

/*
** Defines function prototypes
*/

time_t Second( );
time_t MakeTime(  );

char *LTrim( );
char *RTrim( );
void All2Upper( );

int  pipe(), fork();
void AbortSig();
void AlarmSig();
void HexDump( );

/*
** Defines Global Var
*/
int g_sig = 0;

/*
** Main Process
*/
main(argc,argv)
int argc;
char *argv[];
{
        INFO_T info;

        if ( argc != 3 || strcmp(argv[1],TAG_MSC) )
        {
                fprintf(stderr,"Usage: %s MSC MSC_NO\n\tex> %s MSC 101\n", argv[0], argv[0]);
                fflush(stderr);
                exit(FAILURE);
        }

        g_sig = 0;
        memset(&info, 0x00, sizeof(info));
        info.argv[0] = argv[1]; /* MSC                 */
        info.argv[1] = argv[2]; /* MSC_NO        */
        info.systime = time(0);
        sprintf(info.r_host, "%s%s", argv[1], argv[2]);

        if ( info.systime < 0 )
        {
                SYSERR("time()", errno);
                exit(FAILURE);
        }

        if ( GetConfig(&info) )        /* 원격 정보를 읽는다. */
        {
                fprintf(stderr, "GetConfig에 미등록된 %s\n", info.r_host);fflush(stderr);
                SysErrorLog(info.r_host, "GetConfig에 미등록\n");
                exit(FAILURE);
        }

        if ( GetCmds(&info) )        /* 명령 정보를 읽는다. */
        {
                fprintf(stderr, "GetCmds 오류 %s\n", info.r_host);fflush(stderr);
                exit(FAILURE);
        }

        telnet_login(&info);

        exit(0);
}


/******************************************************************************
*                                                                                                                                                          *
*        함수명 : telnet_login                                                                                                          *
*          기능 : Process를 fork하여 tenete으로 login으로 한다.                                          *
*        작성자 : [배재영]                                                                                                                  *
*        작성일 : 2005/01/01                                                                                                                    *
*  파라메터 :IN/OUT  PARAM NAME  TYPE           DESCRIPTION                                                     *
*                         ------  ----------  --------  ---------------------------------  *
*                          [I]         info                 INFO_T *  원격지 정보                                                  *
*        리턴값 :                                                                                                                                  *
*  주의사항 : pipe를 사용하여 명령을 내리고, 결과를 받는다.                                             *
*  [ 변 경 사 항 ]----------------------------------------------------------- *
*  작성일자                작성자                                          작성내용                                                             *
*  ----------   --------------   -------------------------------------------- *
*  2005/01/01          B.J.Y                 초기작성                                                                                   *
******************************************************************************/

telnet_login(info)
INFO_T *info;
{
        int  fd1[2], fd2[2], ps_id;
        int  ret;

        signal(SIGINT, AbortSig);
        signal(SIGUSR1,AbortSig);
        signal(SIGHUP, AbortSig);
        signal(SIGCLD, AbortSig);   /* Child 프로세스가 죽을 경우 Catch 됨.*/

        if ( pipe(fd1) < 0 || pipe(fd2) < 0 || (ps_id = fork()) < 0 )
        {
                SYSERR("pipe or fork", errno);
                return(FAILURE);
        }

        if ( ps_id > 0 )        /*** parent process ***/
        {
                if ( close(fd1[0]) )
                {
                        SYSERR("close(fd1[0])", errno);
                }
                if ( close(fd2[1]) )
                {
                        SYSERR("close(fd2[1])", errno);
                }

                sleep(1);
                if ( g_sig == 0 )
                {
                        waitpid(ps_id,NULL,WNOHANG);
                        ParsingData(info, fd1[1], fd2[0]);  /* Send Cmd, Receive Result */
                } else {
                        SysErrorLog(info->r_host, "ALL PORT IN USE\n");
                }

                if ( close(fd1[1]) )
                {
                        SYSERR("close(fd1[1])", errno);
                }
                if ( close(fd2[0]) )
                {
                        SYSERR("close(fd2[1])", errno);
                }

                printf("kill [%d]\n", ps_id); fflush(stdout);
                if ( kill(ps_id, SIGTERM) )         { SYSERR("kill", errno); }

        } else {/******* child process ***********/
                if ( close(fd1[1]) )
                {
                        SYSERR("close(fd1[1])", errno);
                }
                if ( close(fd2[0]) )
                {
                        SYSERR("close(fd2[0])", errno);
                }

                if ( fd1[0] != STDIN_FILENO )
                {
                        if ( dup2(fd1[0],STDIN_FILENO) != STDIN_FILENO )
                        {
                                SYSERR("dup2(fd1[0])", errno);
                        }
                        if ( close(fd1[0]) )
                        {
                                SYSERR("close(fd1[0])", errno);
                        }
                }

                if ( fd2[1] != STDOUT_FILENO )
                {
                        if ( dup2(fd2[1],STDOUT_FILENO) != STDOUT_FILENO )
                        {
                                SYSERR("dup2(fd2[1])", errno);
                        }
                        if ( close(fd2[1]) )
                        {
                                SYSERR("close(fd2[1])", errno);
                        }
                }

                fprintf(stderr,"r_host=%s, user_id=%s\n", info->r_host, info->userid );
                fflush(stderr);
                /*********************** EIP LOG DATA TAIL ***********************/

                ret = execl("/usr/bin/telnet", "telnet", info->r_host, (char *) 0);
                if ( ret )
                {
                        SYSERR("execl", errno);
                }
        } /* end if */
}


/******************************************************************************
*                                                                                                                                                          *
*        함수명 : ParsingData                                                                                                          *
*          기능 : telnet으로 login하여 kill 명령湧?수행 시킨다.                                          *
*        작성자 : [배재영]                                                                                                                  *
*        작성일 : 2005/01/01                                                                                                                    *
*  파라메터: IN/OUT  PARAM NAME  TYPE           DESCRIPTION                                              *
*                         ------  ----------  --------  ---------------------------------  *
*                          [I]         info                 INFO_T *  원격지 정보                                                  *
*                          [I]         fd1_w             int           Command를 내리는 파일 포인터                    *
*                          [I]         fd2_r             int           결과를 받는 파일 포인터                             *
*        리턴값 :                                                                                                                                  *
*  주의사항 : pipe를 사용하여 명령을 내리고, 결과를 받는다.                                      *
*  [ 변 경 사 항 ]----------------------------------------------------------- *
*  작성일자                작성자                                          작성내용                                                             *
*  ----------   --------------   -------------------------------------------- *
*  2005/01/01          B.J.Y                 초기작성                                                                              *
******************************************************************************/

ParsingData(info, fd1_w, fd2_r)
INFO_T *info;
int fd1_w, fd2_r;
{
        register int i;

        int  nRet;
        char *tok[BUFSIZ];
        char line[BUFSIZ];

        SysErrorLog(info->r_host, "Starting\n");

        signal(SIGKILL, AbortSig);
        signal(SIGTERM, AbortSig);
        signal(SIGALRM, AbortSig);

        sleep(1);
        if ( nRet = Login(info, fd1_w, fd2_r, line) ) /* Login시 출력 메시지를 받아낸다. */
        {
                if ( g_sig == 18 )                                                 /* Unknown host */
                {
                        if ( (time(0) - info->systime) < 5 )
                        {
                                SysErrorLog(info->r_host, "UNKNOWN HOST\n");
                        } else {
                                SysErrorLog(info->r_host, "CONNECTION TIMED OUT\n");
                        }
                } else {
                        SysErrorLog(info->r_host, line);
                }
                return(-1);
        }

        for ( i = 0, tok[i] = (char *)strtok(info->cmds, "\n");
                        i < BUFSIZ && tok[i];
                        i++, tok[i] = (char *)strtok(NULL, "\n") )
        {
                if ( CmdExec(tok[i], fd1_w, fd2_r) < 0 )
                {
                        fprintf(stderr, "Error CmdExec\n");
                   &
조회 수 :
1176
추천 수 :
33 / 0
등록일 :
2006.02.09
21:37:50 (*.78.103.207)
엮인글 :
http://bestceok.com/xe/index.php?mid=pds&document_srl=6747&act=trackback&key=943
게시글 주소 :
http://bestceok.com/xe/index.php?mid=pds&document_srl=6747
List of Articles
번호 제목 글쓴이 날짜 조회 수
54 한글 영문 변환 프로그램.... file 하록 2012-07-12 367
53 Usb to Serial Driver All Version file 하록 2009-10-28 1965
52 Usb To Serial Vista 드라이버... file 하록 2008-10-09 1038
51 터미널서버 프로그램 file 하록 2007-08-24 972
50 USB FDD 드라이버 file 하록 2006-10-01 1104
49 portbase 메뉴얼 file 하록 2006-08-10 1019
48 터미널서버 메뉴얼 file 하록 2006-08-10 1120
47 랜덤 출력 소스...(빙고게임) [2] 하록 2006-02-09 1485
» Password Auto 변경... file 하록 2006-02-09 1176
45 사다리게임... [1] [7] file 하록 2005-07-18 1608
44 Usb to Serial Driver file 하록 2005-01-06 1221
43 Love Actually의 한 장면.. 하록 2004-01-08 1030
42 HPCP 교재 요약 최민수 2003-07-11 1783
41 HPCP 덤프 모음 최민수 2003-07-11 1726
40 서버 위치를 지도로 "VisualRoute" v7.1d build 1962 file 하록 2003-05-24 1396
39 디아시디키 빼는거 [2] file 강원걸 2002-01-02 4881
38 유닉스 소스 file 강원걸 2001-12-05 1144
37 데이타베이스 병행수행 강원걸 2001-12-03 1127
36 넷버스 1.7 한글판 [7] [2] file 강원걸 2001-11-24 11783
35 유닉스 통신 프로그램 소스 file 강원걸 2001-11-06 1382