I am trying to write a script that will check all used IP on the server and then print me an addressees that are not in use. Problem is in comparing two variables
echo "1" | awk '
{
c=split(ENVIRON["APACHE"], apache, "\n");
d=split(ENVIRON["IFCONFIG"], ifconfig, "\n");
for (i=1; i<=c; i++) {apa[apache ]=apache ; all[apache ]=apache ; }
for (i=1; i<=d; i++) {ifc[ifconfig]=ifconfig; all[ifconfig]=ifconfig; }
for (i in all) {
if (i in ifc && i in apa) common=i;
if (i in ifc && !(i in apa)) ifc_only=i;
if (!(i in ifc) && i in apa) apa_only=i;
}
for (i in common ) print "Common IPs : " i;
for (i in apa_only) print "In APACHE only : " i;
for (i in ifc_only) print "In IFCONFIG only: " i;
}
'
This might help you find all addresses; the evaluation in the END section could be done (among others) like rdrtx1 proposed:
{ netstat -ntp; ifconfig -a; cat apache2/*; } 2> /dev/null | awk '
/inet addr/ && !/127/ {sub (/addr:/, _, $2); I[$2]++}
$1 == "tcp" {sub (/:.*$/, _, $4); N[$4]++}
/<VirtualHost / {sub (/:.*$/, _, $2); A[$2]++}
END {
for (n in N) print "netstat: ", n, N[n]
for (i in I) print "ifconfig:", i, I
for (a in A) print "apache2: ", a, A[a]
}
'
---------- Post updated at 11:50 ---------- Previous update was at 11:33 ----------
Or, try
{ netstat -ntp; ifconfig -a; cat apache2/*; } 2> /dev/null | awk '
/inet addr/ && !/127/ {sub (/addr:/, _, $2); I[$2]++}
$1 == "tcp" {sub (/:.*$/, _, $4); N[$4]++}
/<VirtualHost / {sub (/:.*$/, _, $2); A[$2]++}
END {for (n in N) {print ((n in I)?((n in A)?"ALL":"N&I"):"NET") ":", n
delete I[n]
delete A[n]
}
for (i in I) {print ((i in A)?"I&A":"IFC") ":", i
delete A
}
for (a in A) print "APA" ":", a
}
'
---------- Post updated at 12:06 ---------- Previous update was at 11:50 ----------
Above doesn't recognize IPs in netstat and apache but not in ifconfig . Try instead
END {for (n in N) {NI = (n in I)
NA = (n in A)
print (NI?(NA?"ALL":"N&I"):(NA?"N&A":"NET")) ":", n