#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;
}
libcli的应用实例
需积分: 12 56 浏览量
2018-04-26
21:27:08
上传
评论
收藏 3KB ZIP 举报
Freely~
- 粉丝: 53
- 资源: 5
最新资源
- 基于Pyortch+python+三种卷积神经实现的深度神经网络的交通标志识别算法python源码+文档说明+数据集
- 深度学习课程设计-基于虚拟仿真环境下的自动驾驶交通标志识别python源码+文档说明+数据+模型权重
- 互联网金融第二章——互联网金融支付-PPT.ppt
- 基于OpenCV实现的交通标志识别C++源码+文档说明+测试图片(课程设计)
- 基于虚拟仿真环境下的自动驾驶交通标志识别python源码+文档说明+截图演示+数据集+使用教学(98分高分毕业设计)
- python实现的基于CNN深度学习网络的交通标志识别+源代码+文档说明+安装教程+使用教程(高分毕业设计)
- 基于Spring Bootd实现的图像去雾系统端,完成主要的前后端交互+源代码+文档说明
- 企业网站建设-PPT.ppt
- 办公自动化Microsoft-Office学习教程.doc
- 办公软件ECEL技巧培训课件-PPT.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈