Script to find available IP address


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to find available IP address
# 1  
Old 03-17-2016
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

Code:
#!/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

Last edited by nemesis911; 03-17-2016 at 10:37 AM.. Reason: ops sorry...didn't edit it.
# 2  
Old 03-17-2016
What's the issue are you facing with diff command here ?
Could you please post i/p and desire o/p ?
# 3  
Old 03-17-2016
try something like:
Code:
echo "1" | awk '
{
   c=split(ENVIRON["APACHE"], apache, "\n");
   d=split(ENVIRON["IFCONFIG"], ifconfig, "\n");

   for (i=1; i<=c; i++) {apa[apache  [i]]=apache[i]  ; all[apache  [i]]=apache[i]  ; }
   for (i=1; i<=d; i++) {ifc[ifconfig[i]]=ifconfig[i]; all[ifconfig[i]]=ifconfig[i]; }

   for (i in all) {
      if (i in ifc && i in apa) common[i]=i;
      if (i in ifc && !(i in apa)) ifc_only[i]=i;
      if (!(i in ifc) && i in apa) apa_only[i]=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;
}
'

# 4  
Old 03-18-2016
hmmm...it doesn't work, this is awk science fiction for me. Reading trough man to try figure out

Code:
#!/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  [i]]=used[i]; all[used[i]]=used[i];}
    for (i=1; i<=d; i++) {ifc[ifconfig[i]]=ifconfig[i]; all[ifconfig[i]]=ifconfig[i];}

    for (i in all) {
       if (i in ifc && i in use) common[i]=i;
       if (i in ifc && !(i in use)) ifc_only[i]=i;
       if (!(i in ifc) && i in use) use_only[i]=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;
}'


Last edited by nemesis911; 03-18-2016 at 06:03 AM..
# 5  
Old 03-18-2016
hmmm... echo "1" | shown in previous post is needed. Otherwise nothing will print.
# 6  
Old 03-24-2016
I tried with echo 1, but there is no output for arrays Common IPs, In APACHE only, In IFCONFIG only Smilie
# 7  
Old 03-25-2016
This might help you find all addresses; the evaluation in the END section could be done (among others) like rdrtx1 proposed:
Code:
{ 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[i]
                         for (a in A) print "apache2: ", a, A[a]
                        }
'

---------- Post updated at 11:50 ---------- Previous update was at 11:33 ----------

Or, try
Code:
{ 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[i]
                                        }
                         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
Code:
END                     {for (n in N)   {NI = (n in I)
                                         NA = (n in A)
                                         print (NI?(NA?"ALL":"N&I"):(NA?"N&A":"NET")) ":", n


Last edited by RudiC; 03-25-2016 at 07:58 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

HELP simple script to find e-mail address on a file

Hello guys, im new to to unix/linux i have a text file like this: person1@test.com iisiiasasas person2@test.com 123w2 3233 sajsja person3@test.com jsajjsa sajsjasaj person4@test.com I want to extract only e-mail address and get rid of all other stuff, i want an output like this ... (4 Replies)
Discussion started by: RazorMX
4 Replies

2. UNIX for Dummies Questions & Answers

How to Find another IP address

Hi Guys, I want to find another(Local host) machine IP address from my machine.. We are all connected in the same server, I mean same LAN. What i done is Connected with that user machine using SSH and SU.. After that i used IFCONFIG but it shows my Ip address. Can not able to get the... (8 Replies)
Discussion started by: Adhi
8 Replies

3. Red Hat

How to find file which stores IP address

Hi all, I am new to this forum and this is my first question :). Using Red hat Linux.I tried to find file which stores IP address based on different helps given in this forum but did not get success. Here is the system details: -bash-3.2$ lsb_release -a LSB Version: ... (2 Replies)
Discussion started by: Barkha
2 Replies

4. IP Networking

How to find the IP address of a computer that I am connected to?

Hi all, One of my computers (an iMac) was recently stolen, but I have been able to connect to it over the internet through Apple's 'back to my mac' feature. In order for the police to find these people they need to know the IP address of the stolen computer. I suspect that I will have find the... (6 Replies)
Discussion started by: RJFoulston
6 Replies

5. AIX

How to find the router IP address

How to find the router IP address (2 Replies)
Discussion started by: AIXlearner
2 Replies

6. UNIX for Advanced & Expert Users

Find IP Address of a Printer Q in Hp-Unix

Could anyone help me to identify the IP address of a printer queue with its printer queue name information . (1 Reply)
Discussion started by: lenin1985
1 Replies

7. HP-UX

how to find the IP address of a printer

I'm trying to find the IP address of a print queue. I tried this, but nada. cat /etc/hosts | lp -d lp01 Looked in the hosts file but it's not there (2 Replies)
Discussion started by: abNORMal
2 Replies

8. UNIX for Advanced & Expert Users

Find LAN Address

Hi, I am wondering how I can find the network address under following situation. Things available: 1. An active network drop without knowing any details about the network. 2. I have windows, Linux, and Solaris machine available to use. Things to achieve: 1. Find out the... (5 Replies)
Discussion started by: jiashi2
5 Replies

9. UNIX for Dummies Questions & Answers

How to find i.p address of our server

I don't have root access. How do I find i.p address of our sun server? Thanks. (4 Replies)
Discussion started by: vpotluri
4 Replies

10. Programming

Find the IP address that a telnet session uses

Hello Experts, Happy New Year to all of us, In AIX 4.3.3 I am trying to figure out how is possible to find out the IP address that a telnet session - user uses to login in to host machine. My objective is by finding the login IP address to allow the user to login or not. All users uses the... (6 Replies)
Discussion started by: sszago
6 Replies
Login or Register to Ask a Question