#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <memory.h>
#include <malloc.h>
#include <time.h>
#include <math.h>
#include <stdarg.h>
#include <sys/types.h>
#include <stdlib.h>
#include <pthread.h>
//#include <sys/time.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <math.h>
#include <errno.h>
#define LOG_WARNING(s, args...) printf("\033[33;48m[%s->%d]"s"\033[0m\n", __FILE__, __LINE__, ##args);
#define RED "31"
#define GREEN "32"
#define YELLOW "33"
#define BLUE "34"
#define PURPLE "35"
#define QINGLAN "36"
#define MARKG(s) printf("\033[32;48m%s\033[0m", s)
#define PRINTG(s, args...) printf("\033[32;48m"s"\n\033[0m", ##args)
#define COLOR_STR(str, corlor) "\033["corlor";48m"str"\033[0m"
#define MARK(s, args...) printf("In %s line %d, "s"\n", __FILE__, __LINE__, ##args);fflush(stdout)
#define LOG_ERROR(s, args...) {\
char _error_log[1024];\
sprintf(_error_log, "[Error] in %s(%s) line %d, "s"\n", __FILE__, __FUNCTION__, __LINE__, ##args);\
logilx(_error_log, "/var/log/error_cba_v2.log");\
printf("\033[31;48m%s\033[0m\n", _error_log);\
}
#define CPR(s, args...) printf(COLOR_STR(s, RED), ##args)
#define CPG(s, args...) printf(COLOR_STR(s, GREEN), ##args)
#define CPY(s, args...) printf(COLOR_STR(s, YELLOW), ##args)
#define CPP(s, args...) printf(COLOR_STR(s, PURPLE), ##args)
#define CPB(s, args...) printf(COLOR_STR(s, BLUE), ##args)
#define CPQ(s, args...) printf(COLOR_STR(s, QINGLAN), ##args)
char** split(char *strin, char *seprator, int *l, int max) {
char **rtn;
rtn = (char **) malloc(sizeof(char **));
char *str = strdup(strin);
char *str0 = str;
char *p;
p = strstr(str, seprator);
if (p == NULL) {
*l = 1;
*rtn = str;
return rtn;
}
int i, len, spl, inlen, plen;
spl = strlen(seprator);
max--;
*l = 0;
while (p != NULL) {
if (*l > 0)
rtn = realloc(rtn, ((*l) + 1) * sizeof(char **));
inlen = strlen(str);
plen = strlen(p);
if (!plen)
break;
len = inlen - plen;
rtn[*l] = malloc(len + 1);
strncpy(rtn[*l], str, len);
rtn[*l][len] = 0;
str += (len + spl);
p = strstr(str, seprator);
(*l)++;
if (*l == max || p == NULL) {
/*if (*l == max) {
len = strlen(rtn[*l - 1]) + strlen(str) + 1;
rtn[*l - 1] = realloc(rtn[*l - 1], len);
strncat(rtn[*l - 1], str, len - 1);
} else {*/
len = strlen(str);
rtn = realloc(rtn, ((*l) + 1) * sizeof(char **));
rtn[*l] = malloc(len + 1);
strncpy(rtn[*l], str, len);
rtn[*l][len] = 0;
//}
(*l)++;
break;
}
}
free(str0);
return rtn;
}
void exeCmd(char* cmd, char* rtn, int lenRtn){
char line_buf[256]={0};
FILE *fp = popen(cmd, "r");
if(fp == NULL){
return;
}
rtn[0] = 0;
while(!feof(fp))
{
if(fgets(line_buf, sizeof(line_buf), fp)){
if(strlen(line_buf)+strlen(rtn)<lenRtn){
strcat(rtn, line_buf);
}else break;
}else break;
}
pclose(fp);
}
char* doReadFile(char* filename, int limit, int * size) {
errno = 0;
char buff_path[256];
getcwd(buff_path, sizeof(buff_path));
strcat(buff_path, "/");
strcat(buff_path, filename);
*size = 0;
int len = 128;
if(limit<len*2) limit = len *2;
char *buffer = malloc(limit + 1);
buffer[0] = 0;
FILE *fp;
if ((fp = fopen(buff_path, "r")) == 0) {
free(buffer);
buffer = NULL;
return NULL;
}
rewind(fp);
int pos = 0;
int l;
char* buf = malloc(len);
while (!feof(fp)) {
l = fread(buf, 1, len, fp);
pos += l;
if (l > 0)
strncat(buffer, buf, l);
if (pos + len > limit) {
// LOG_ERROR("filesize %s bigger than %d\n", buff_path, limit);
break;
}
}
*size = pos;
free(buf);
//printf("Read %s, data length: %d\n", filename, pos);
fclose(fp);
buffer[pos - 1] = '\0';
return buffer;
}
void split_free(char**rtn, int l){
int i;
for(i=0;i<l;i++){
free(rtn[i]);
}
free(rtn);
}
void writeFile(char* filename, char* data, int len, int append) {
errno = 0;
char buff_path[256];
getcwd(buff_path, sizeof(buff_path));
strcat(buff_path, "/");
strcat(buff_path, filename);
FILE *fp = fopen(buff_path, append > 0 ? "a+" : "w+");
//MARK("buff_path=%s", buff_path);
if (fp==NULL) {
// LOG_ERROR("Write file %s failed, %s", buff_path, strerror(errno));
return;
}
int w = fwrite(data, 1, len, fp);
fflush(fp);
//MARK("Write file %s may OK,need write %d=w %d, errno=%d", buff_path, len, w, errno);
fclose(fp);
}
void now_time_str(char* str1,int str1_len){
time_t timep;
struct tm systm;
time(&timep);
timep += 8 * 3600;
char str[48];
//------------------------------------------
gmtime_r( &timep, &systm );
//sprintf(str,"%04d%02d%02d%02d:%02d",systm.tm_year+1900,systm.tm_mon+1,systm.tm_mday,systm.tm_hour,systm.tm_min,systm.tm_sec);
snprintf(
str, sizeof(str), "%04d-%02d-%02d %02d:%02d:%02d",
systm.tm_year+1900,systm.tm_mon+1,systm.tm_mday,
systm.tm_hour,systm.tm_min,systm.tm_sec
);
if(str1_len<strlen(str))
{
strncpy(str1, str, str1_len-1);
str1[str1_len-1] = 0;
}else strcpy(str1, str);
}
void main(int argc, char** argv){
char cfg_file[128];
char base_trace_cmd[128];
strcpy(cfg_file, "ips.txt");
strcpy(base_trace_cmd, " -U -m 10 ");
char cmd[128];
sprintf(cmd, "rm -f *.tmp");
system(cmd);
CPB("Usage %s [ip config file] [\"trace param\"]\n", argv[0]);
if(argc>1) strcpy(cfg_file, argv[1]);
if(argc>2) strcpy(base_trace_cmd, argv[2]);
int file_size = 0;
char *cnts = doReadFile(cfg_file, 4096, &file_size);
int count = 0;
char *ip;
char** ips = split(cnts, "\n", &count, 1024);
char** pids = malloc(count*sizeof(char*));
int i;
printf("Got %d ips\n", count);
for(i=0;i<count;i++) {
ip = ips[i];
if(ip[strlen(ip)-1]==13){
ip[strlen(ip)-1] = 0;
}
pid_t pid = fork();
if(pid==0){
char cmd[128];
char rtn[1024];
sprintf(cmd, "traceroute %s-p %d %s", base_trace_cmd, 33434+(i%200), ip);
printf("[%u] %s ...\n", getpid(), cmd);
exeCmd(cmd, rtn, sizeof(rtn));
sprintf(cmd, "tra_%s.tmp", ip);
writeFile(cmd, rtn, strlen(rtn), 0);
exit(0);
} else if(pid>0) {
pids[i] = ip;
CPG("[%d] %s\n", i, pids[i]);
} else exit(0);
}
CPG("All ips started\n");
free(cnts);
int left = count;
char *p;
now_time_str(cmd, sizeof(cmd));
strcat(cmd, ": \r\n");
writeFile("ok_ips.txt", cmd, strlen(cmd), 1);
writeFile("no_ips.txt", cmd, strlen(cmd), 1);
for(i=0;i<count;i++) {
sprintf(cmd, "tra_%s.txt", pids[i]);
CPR("[%d] %s\n", i, cmd);
}
while(left>0){
for(i=0;i<count;i++) {
if(strlen(pids[i])==0) continue;
sprintf(cmd, "tra_%s.tmp", pids[i]);
cnts = doReadFile(cmd, 1024, &file_size);
if(file_size>0) {
p = strchr(cnts, '\n');
if(p==NULL) {
printf("error(not chr(10)): %s\n", cnts);
exit(0);
}
p = strstr(p, pids[i]);
sprintf(cmd, "%s,", pids[i]);
if(p!=NULL) {
writeFile("ok_ips.txt", cmd, strlen(cmd), 1);
printf("%s ok\n", cmd);
}else{
writeFile("no_ips.txt", cmd, strlen(cmd), 1);
printf("%s fail\n", cmd);
}
left --;
pids[i][0] = 0;
}//else CPY(" > %s\n", cmd);
free(cnts);
}
CPY("Working...\n");
sleep(1);
}
writeFile("ok_ips.txt", "\n", 1, 1);
writeFile("no_ips.txt", "\n", 1, 1);
split_free(ips, count);
}
gcc -o multi_trace multi_trace.c