libcli应用

libcli应用

通常在我们的程序运行中,我们需要对程序运行状态,变量值等情况进行了解,此时我们就可以用到Libcli库。

libcli可以从此处下载:http://freshmeat.sourceforge.net/projects/libcli

目录结构:

[root@smart myself]# tree
.
├── build.sh
├── cli
├── cli.c
├── cli_debug.c
├── cli_debug.h
├── cli.h
└── main.c

cli.c

#include "cli.h"
#include "cli_debug.h"

#define CLI_PORT                    9999
#define MODE_CONFIG_INT             10

#ifdef __GNUC__
# define UNUSED(d) d __attribute__ ((unused))
#else
# define UNUSED(d) d
#endif

unsigned int regular_count = 0;
unsigned int debug_regular = 0;



int check_auth(char *username, char *password)
{
    if (strcasecmp(username, "root") != 0)
        return CLI_ERROR;
    if (strcasecmp(password, "root") != 0)
        return CLI_ERROR;
    return CLI_OK;
}

int regular_callback(struct cli_def *cli)
{
    regular_count++;
    if (debug_regular)
    {
        cli_print(cli, "Regular callback - %u times so far", regular_count);
        cli_reprompt(cli);
    }
    return CLI_OK;
}

int check_enable(char *password)
{
    return !strcasecmp(password, "root");
}

void pc(struct cli_def *cli, char *string)
{
    printf("%s\n", string);
}

int cli_handle()
{
    pthread_t tid;
    printf ("Enter Cli handle functions.\n");
    printf ("Cli ip:port %s %d\n","Any",CLI_PORT);
    printf ("User: %s  passwd:%s\n","root","root");


    int ret = 0;
    ret = pthread_create(&tid, NULL, cli_main, (void *)NULL);
    if (0 != ret)
    {
        printf ("error:pthread_create\n");
        return 1;
    }
    pthread_join(tid, 0);

    return 0;
}

int cli_main()
{
    struct cli_command *c;
    struct cli_def *cli;
    int s, x;
    struct sockaddr_in addr;
    int on = 1;
    signal(SIGCHLD, SIG_IGN);    
    cli = cli_init();
    cli_set_banner(cli, "dp_mod debug CLI console");
    cli_set_hostname(cli, "Smart");
    cli_regular(cli, regular_callback);
    cli_regular_interval(cli, 5); // Defaults to 1 second
    cli_set_idle_timeout(cli, 600); // 60 second idle timeout

    cli_register_funcs(cli);

    cli_set_auth_callback(cli, check_auth);

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("socket");
        return 1;
    }
    setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(CLI_PORT);
    if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
    {
        perror("bind");
        return 1;
    }

    if (listen(s, 50) < 0)
    {
        perror("listen");
        return 1;
    }

    printf("Listening on port %d\n", CLI_PORT);
    while ((x = accept(s, NULL, 0)))
    {
        int pid = fork();
        if (pid < 0)
        {
            perror("fork");
            return 1;
        }

        /* parent */
        if (pid > 0)
        {
            socklen_t len = sizeof(addr);
            if (getpeername(x, (struct sockaddr *) &addr, &len) >= 0)
                printf(" * accepted connection from %s\n", inet_ntoa(addr.sin_addr));

            close(x);
            continue;
        }

        /* child */
        close(s);
        cli_loop(cli, x);
        exit(0);
    }

    cli_done(cli);
    return 0;
}

cli.h

#ifndef __CLI_H__
#define __CLI_H__
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <libcli.h>

extern int check_auth(char *username, char *password);
extern int check_enable(char *password);

int cli_handle();

extern int cli_main();

void pc(struct cli_def *cli, char *string);

extern int regular_callback(struct cli_def *cli);

#endif /* __CLI_H__ */

cli_debug.c

#include "cli.h"
#include "cli_debug.h"

int cmd_test(struct cli_def *cli, char *command, char *argv[], int argc)
{
    int i;
    cli_print(cli, "called %s with \"%s\"", __FUNCTION__, command);
    cli_print(cli, "%d arguments:", argc);
    for (i = 0; i < argc; i++)
        cli_print(cli, "        %s", argv[i]);

    return CLI_OK;
}


void cli_register_funcs(struct cli_def *cli)
{
    cli_register_command(cli, NULL, "test", cmd_test, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
}

cli_debug.h

#ifndef __CLI_DEBUG_H__
#define __CLI_DEBUG_H__

extern void cli_register_funcs(struct cli_def *cli);
extern int cmd_test(struct cli_def *cli, char *command, char *argv[], int argc);

#endif /* __CLI_DEBUG_H__ */

main.c

#include "cli.h"
#include "cli_debug.h"

int main()
{
    cli_handle();
    return 0;
}

编译:gcc -o cli cli.c cli_debug.c main.c -lcli -lpthread

下载链接:https://download.csdn.net/download/juyin2015/10377086

欢迎留言指正!


Juyin@2018/4/26

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值