Script to find available IP address

Hi,

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

#!/bin/bash

NETSTAT=$(netstat -ntp | awk '{ print $4 }' | grep -v "127.0.0.1" | cut -d ":" -f1 | grep "^[0-9]"|sort | uniq )
APACHE=$(grep -R "<VirtualHost " /etc/apache2/* | cut -d ":" -f2 | cut -d " " -f2 |  cut -d ">" -f1 | grep "^[0-9]" | sort | uniq)

USED=("${NETSTAT[@]}\n${APACHE[@]}")
IFCONFIG=$(ifconfig -a | grep -v "127.0.0.1" | grep addr | awk '{ print $2 }' | cut -d ":" -f2 | grep "\.")

???

I don't want to use diff because I want to use loop which will give me better output

What's the issue are you facing with diff command here ?
Could you please post i/p and desire o/p ?

try something like:

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;
}
'

hmmm...it doesn't work, this is awk science fiction for me. Reading trough man to try figure out

#!/bin/bash

NETSTAT=$(netstat -ntp | awk '{ print $4 }' | grep -v "127.0.0.1" | cut -d ":" -f1 | grep "^[0-9]")
APACHE=$(grep -R "<VirtualHost " /etc/apache2/* | cut -d ":" -f2 | cut -d " " -f2 |  cut -d ">" -f1 | grep "^[0-9]")

USE=("${NETSTAT[@]}\n${APACHE[@]}")
USED=$( printf "${USE[@]}" | sort | uniq)
IFCONFIG=$(ifconfig -a | grep -v "127.0.0.1" | grep addr | awk '{ print $2 }' | cut -d ":" -f2 | grep "\.")


awk '{
    c=split(ENVIRON["USED"], used, "\n");
    d=split(ENVIRON["IFCONFIG"], ifconfig, "\n");
 
    for (i=1; i<=c; i++) {use[used  ]=used; all[used]=used;}
    for (i=1; i<=d; i++) {ifc[ifconfig]=ifconfig; all[ifconfig]=ifconfig;}

    for (i in all) {
       if (i in ifc && i in use) common=i;
       if (i in ifc && !(i in use)) ifc_only=i;
       if (!(i in ifc) && i in use) use_only=i;
    }
 
    for (i in common  ) print "Common IPs      : " i;
    for (i in use_only) print "In APACHE only  : " i;
    for (i in ifc_only) print "In IFCONFIG only: " i;
}'

hmmm... echo "1" | shown in previous post is needed. Otherwise nothing will print.

I tried with echo 1, but there is no output for arrays Common IPs, In APACHE only, In IFCONFIG only :frowning:

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