我正在为Linux下的nmap编写一个简单的ncurses GUI包装器,以便更容易阅读和理解输出。但是,在解析输出时,使用POSIX正则表达式并评估代码中的每个表达式,或者将nmap输出传递给grep
,sed
或cut
等实用程序更快。 ?
例如,如果我想检索子网中的在线主机,以下哪种解决方案可能会更好?
pipe = popen("nmap -sn xxx.xxx.xxx.xxx/24", "r");
if (pipe == NULL) {
fprintf (stderr, "error creating the pipe: %s\n", strerror(errno));
exit (1);
}
while (!feof (pipe)) {
if (fgets (buff, BUFF_SIZE, pipe) != NULL) {
/* perform regex evaluations here */
printf ("%s", buff);
}
}
pclose (pipe);
VS
pipe = popen("nmap -sn xxx.xxx.xxx.xxx/24 | grep -E 'pattern' | ...", "r");
if (pipe == NULL) {
fprintf (stderr, "error creating the pipe: %s\n", strerror(errno));
exit (1);
}
while (!feof (pipe)) {
if (fgets (buff, BUFF_SIZE, pipe) != NULL) {
printf ("%s", buff);
}
}
pclose (pipe);