实例介绍
【实例截图】
【核心代码】
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <getopt.h> #include "Ip_func.h" #include "Ip_detect.h" //分配空间,如果分配失败则等待5秒继续分配, //连续分配5次失败后退出,分配成功则返回地址; void *emalloc(size_t size) { void *ptr; /* * Just for our personal safety, we increase the * size by one */ if((int)size < 0) { fprintf(stderr, "[%d] Won't allocate a pointer of size %u !\n", getpid(), size); } size ; /* * If no memory can be allocated, then wait a little. * It's very likely that another process child will free * the size of memory we need. So we make 10 attempts, * and if nothing happens, then we exit */ ptr = malloc(size); if(!ptr) { int i; for(i = 0; i < 5 && ptr == NULL; i ) { waitpid(0, NULL, WNOHANG); usleep(5000); ptr = malloc(size); } if( ptr == NULL ) { fprintf(stderr, "[%d] Could not allocate a pointer of size %u !\n", getpid(), size); exit(1); } } bzero(ptr, size); return ptr; } char *estrdup(const char *str) { char * buf; int len; if(!str) { return NULL; } len = strlen(str); buf = emalloc(len 1); memcpy(buf, str, len); buf[len] = '\0'; return buf; } void efree(void *ptr) { char **p = ptr; if(p && *p) { free(*p); *p=NULL; } } Ip_List *Single_ip(char *Str_ip) { int is_valid; Ip_List *head = NULL; Ip_List *p = NULL; struct in_addr addr; is_valid = inet_pton(AF_INET, Str_ip, &addr); if(is_valid <= 0) { printf("IP invalid....\n"); printf("\tToo know detail using the --help(-h) option\n"); exit(0); } p = (Ip_List *)emalloc(sizeof(Ip_List)); head = p; strcpy(p->Ip_arglist, Str_ip); return head; } Ip_List *Range_ip(char *Str_ip) { char *IP_tail = NULL; char *IP_head = NULL; char *IP_p = NULL; char *buf = NULL; int is_valid; int count = 0; Ip_List *head = NULL; if(strchr(Str_ip, '-') == 0) { printf("IP invalid....\n"); printf("\tToo know detail using the --help(-h) option\n"); exit(0); } buf = estrdup(Str_ip); IP_tail = buf; while(count < 2) { if(!IP_tail) { break; } IP_head = IP_tail; IP_p = strchr(IP_tail, '-'); if(IP_p) { *IP_p = '\0'; IP_tail = IP_p 1; } struct in_addr addr; is_valid = inet_pton(AF_INET, IP_head, &addr); if(is_valid <= 0) { printf("IP invalid....\n"); printf("\tToo know detail using the --help(-h) option\n"); exit(0); } count ; } head = Range_ip_Init(Str_ip); return head; } Ip_List *Range_ip_Init(char *Str_ip) { char *buf = NULL; char tmp[32] = {0}; //char tmp1[32] = {0}; char str_tmp[16] = {0}; Ip_List *head = NULL; Ip_List *p = NULL; Ip_List *q = NULL; int var1[5] = {0}, var2[5] = {0}; if(!Str_ip) { return NULL; } buf = (char *)emalloc(80); memset(buf, '0', 80); strcpy(buf, Str_ip); sscanf(buf, "%d.%d.%d.%d-%d.%d.%d.%d", &var1[0], &var1[1], &var1[2], &var1[3], &var2[0], &var2[1], &var2[2], &var2[3]); sprintf(tmp, "%d.%d.%d.", var1[0], var1[1], var1[2]); //strcmp(tmp1, tmp); p = (Ip_List *)emalloc(sizeof(Ip_List)); head = p; while(var2[3] >= var1[3]) { q = p; sprintf(str_tmp, "%d", var1[3]); strcat(tmp, str_tmp); strcpy(p->Ip_arglist, tmp); var1[3] ; if(var1[3] <= var2[3]) { p = (Ip_List *)emalloc(sizeof(Ip_List)); q->next = p; } else { break; } memset(tmp, '0', 32); sprintf(tmp, "%d.%d.%d.", var1[0], var1[1], var1[2]); } p->next = NULL; return head; } Ip_List *Mask_ip(char *Str_ip) { Ip_List *head = NULL; //Ip_List *p = NULL; char *Ip_tail = NULL; char *Ip_head = NULL; char *Ip_p = NULL; int result = 0; Ip_tail = Str_ip; Ip_head = Ip_tail; Ip_p = strchr(Ip_tail, '/'); *Ip_p = '\0'; Ip_tail = Ip_p 1; if(!Ip_p) { printf("IP invalid....\n"); printf("\tToo know detail using the --help(-h) option\n"); exit(0); } struct in_addr addr; result = inet_pton(AF_INET, Ip_head, &addr); if(result <= 0) { printf("IP invalid....\n"); printf("\tToo know detail using the --help(-h) option\n"); exit(0); } Ip_head = Ip_tail; if(atoi(Ip_head) < 0 || atoi(Ip_head) > 32) { printf("IP invalid....\n"); printf("\tToo know detail using the --help(-h) option\n"); exit(0); } return head; } Ip_List *Is_ip_online(Ip_List *head) { FILE *fp = NULL; Ip_List *p = NULL; char commond[64]; char buffer[1024] = {0}; p = head; while(p) { sprintf(commond, "ping %s -w 1", p->Ip_arglist); //sprintf(commond, "ping %s -w 1 1 > /dev/null 2 > /dev/null", p->Ip_arglist); fp = popen(commond, "r"); while(!feof(fp)) { fgets(buffer, sizeof(buffer) - 1, fp); if(!strstr(buffer, "0 received")) { p->flag = 1; } else { p->flag = 0; break; } } memset(commond, '0', 64); memset(buffer, '0', 1024); pclose(fp); p = p->next; } return head; } void display_help(char *Str_ip) { printf("General options :\n"); printf("\t-v : shows version number\n"); printf("\t-h : shows this help\n"); printf("\t-s : single ip <option> [ip]\n"); printf("\t-r : range ip <option> [ip1-ip2]\n"); printf("\t-m : mask ip <option> [ip1/mask]\n"); exit(0); }
标签: 主机在线个数
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论