libcli应用

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

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

目录结构:

1
2
3
4
5
6
7
8
9
[root@smart myself]# tree
.
├── build.sh
├── cli
├── cli.c
├── cli_debug.c
├── cli_debug.h
├── cli.h
└── main.c

cli.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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

1
2
3
4
5
6
7
#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

1
2
3
4
5
6
7
8
#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

欢迎留言指正!