Yeah, I tried that and unfortunately it did not work.
Code:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
int stdout_fileno = dup(STDOUT_FILENO);
int fd = open("string.txt", O_RDWR | O_CREAT, 0755);
if(fd < 0) {
printf("OPEN(-1) error -> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
dup2(fd, STDOUT_FILENO);
execl("/bin/netstat", "netstat", "-a", "-n", "-t", "-p", ">", "string.txt", (char *)NULL);
dup2(STDOUT_FILENO, stdout_fileno);
close(fd);
FILE *file = fopen("string.txt", "r");
if(file == NULL) {
printf("FOPEN[file](NULL) error -> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
char ch;
while(ch != EOF) {
ch = fgetc(file);
printf("%c", ch);
}
return 0;
}
Just to give you an idea, I am converting my bash script to a C program to tighten up some GUI code.
Code:
netstat -antp |
awk 'NF="7" {print $5"\t""\t""\t""\t""\t"$7}' |
awk 'FS=":" {print $2}' |
awk '!x[$0]++' |
awk 'NF' > netstat.txt;
egrep -v "(443|80)" netstat.txt > netstat2.txt;
LINES=$(awk 'END {print NR}' netstat2.txt);
ONE=1;
if [ $LINES -lt $ONE ]; then
echo "NO SUSPICIOUS PORTS OPEN";
cat netstat.txt;
exit 0;
else
echo "[OPEN PORTS]";
cat netstat2.txt;
sudo warning
fi