OS/vxworks

Vxworks에서 Telnet 클라이언트 제어하기

ppiazi 2010. 12. 12. 13:23

설명

     리모트에 있는 텔넷 서버에 접속하여, 원하는 커맨드를 받고 응답을 받아 수행시키는 프로그램을 작성해야 했다. 텔넷 클라이언트를 직접 구현하지 않았으며, Vxworks에서 기본으로 제공하는 telnet을 쉘에서 수행하여 read/write하는 예제이다. 해당 기능을 확인하는 용도로 만들었으며, 좀더 세밀하여 연결을 유지하고 연결 종료를 확인하는 소스는 추후 업데이트하기로 한다.

 

예제소스

  1. #include <vxWorks.h>
    #include <stdio.h>
    #include <ioLib.h>
    #include <iosLib.h>
    #include <ptyDrv.h>
    #include <shellLib.h>
    #include <tickLib.h>

    #define    MAX_LEN    1024

    extern int shellTaskName;

    char commandBuff[100];
    char recvBuff[100];

    #define TELNET_CMD    "telnet \"10.100.10.100\""
    #define USER_ID        "admin"
    #define    USER_PW        ""
    #define    CMD_EN_MODE_1    "en"
    #define    CMD_EN_MODE_2    ""
    #define    CMD_CONF_MODE    "configure"
    #define    CMD_INT_DISABLE_1    "interface 0/18"
    #define CMD_INT_DISABLE_2    "shutdown"

    char *telnet_connect_proc[64] =
    {
        "telnet \"10.100.10.100\"",    // telnet command
        NULL
    };

    char *telnet_login_proc[64] =
    {
        "admin",
        "",
        NULL
    };

    char *en_mode_proc[64] =
    {
        "en",
        "",
        NULL
    };

    char *conf_mode_proc[64] =
    {
        "configure",
        NULL
    };

    char *int_enable_proc[64] =
    {
        "interface 0/%d",
        "no shutdown",
        "exit",
        NULL
    };

    char *int_disable_proc[64] =
    {
        "interface 0/%d",
        "shutdown",
        "exit",
        NULL
    };

    static int fdSlave, fdMaster;

    void doCmd(char *cmd)
    {
        int commandLen = 0;
       
        commandLen = strlen(cmd);
        strcpy(commandBuff, cmd);
        commandBuff[commandLen] = 0xa; /* '\n' */
        commandLen++;
       
        write(fdMaster, commandBuff, commandLen);
       
        taskDelay(100);
    }

    void doCmdWhArg(char *cmd, int arg)
    {
        int commandLen = 0;
           
        sprintf(commandBuff, cmd, arg);
        commandLen = strlen(commandBuff);
        commandBuff[commandLen] = 0xa; /* '\n' */
        commandLen++;
       
        write(fdMaster, commandBuff, commandLen);
       
        taskDelay(100);
    }

    void doCmdProc(char *cmds[64])
    {
        int i = 0;
       
        while ( cmds[i] != NULL )
        {
            doCmd(cmds[i]);
           
            i++;
        }
    }

    void doCmdDisableInt(int iIntNum)
    {
        int i = 0;
        doCmdWhArg(int_disable_proc[i++], iIntNum);
       
        while ( int_disable_proc[i] != NULL )
        {
            doCmd(int_disable_proc[i]);
           
            i ++;
        }   
    }

    void doCmdEnableInt(int iIntNum)
    {
        int i = 0;
        doCmdWhArg(int_enable_proc[i++], iIntNum);
       
        while ( int_enable_proc[i] != NULL )
        {
            doCmd(int_enable_proc[i]);
           
            i ++;
        }
    }

    int checkLogin(char *buff)
    {
        if ( strstr(buff, "Connected to") != NULL )
        {
            return 1;
        }
        else if ( strstr(buff, "ERR") != NULL )
        {
            return 2;
        }
        else
        {
            return 0;
        }
    }

    int appl()
    {
        int ret;
        char buff[MAX_LEN];
        unsigned int tickStart;
        unsigned int tickEnd;
        unsigned int tickGap;
       
        ptyDevCreate("/pty",1024,1024);
       
        fdSlave = open("/ptyS", O_RDWR, 0);
        fdMaster = open("/ptyM", O_RDWR, 0);
       
        FILE *pFile = fdopen(fdMaster, "r+");

        shellGenericInit("INTERPRETER=C", 0, "tTelnetSession", NULL, FALSE, FALSE, fdSlave, fdSlave, STD_ERR);
       
        tickStart = tickGet();
        doCmdProc(telnet_connect_proc);
       
        fgets(buff, MAX_LEN - 1, pFile);
       
        while ( ( ret = checkLogin(buff) ) == 0 )
        {
            puts(buff);
            fgets(buff, MAX_LEN - 1, pFile);
        }   
       
        tickEnd = tickGet();
        tickGap = tickEnd - tickStart;   
        printf("gap %d\n", tickGap);
       
        if ( ret == 1 )
        {
            printf(">> Connect Success!!\n");

            doCmdProc(telnet_login_proc);
           
            doCmdProc(en_mode_proc);
           
            doCmdProc(conf_mode_proc);
           
            doCmdDisableInt(13);
           
            fgets(buff, MAX_LEN - 1, pFile);       
            puts(buff);
           
            taskDelay(10000);
           
            doCmdEnableInt(13);
           
            fgets(buff, MAX_LEN - 1, pFile);       
            puts(buff);
        }
        else
        {
            printf("Login Fail!!\n");
        }

        //taskDelay(10000);

        fflush(pFile);
       
        close(fdMaster);
        close(fdSlave);
       
        ptyDevRemove("/pty");
           
        printf("Finished\n");
       
        return 0;
    }

 

 

이 글은 스프링노트에서 작성되었습니다.