ErAB
ErAB

Reputation: 915

to write a shell script to grep IP and show to whom it belongs to?

To write a shell script program to grep IP (from the list given) and search the network for the IP whom it belongs to. Then if the IP belongs to vodafone, telstra, airtel, write them in a file separately for each operator.

So i wrote the script should be like :

for ip_address in `cat ip_address_list`;  
do  
whois $ip_address | grep 'descr' >> final_result 
done  

the output looks like :

$] whois 62.87.90.54 | grep descr
descr: GLOBAL MOBILE OPERATOR
descr: AIRTEL-NETWORK
descr: VODAFONE-NETWORK

Can we modify it more ? Can we include IF statement to separate vodafone etc to include them in a file ?

Or pls mention a good script than mine ?

Pls advise !

Upvotes: 1

Views: 2398

Answers (1)

shellter
shellter

Reputation: 37288

for ip_address in $(cat ip_address_list); do  
    whois $ip_address | grep 'descr' \
    | while read line ; do
      case "${line}" in 
         *GLOBAL\ MOBILE\ OPERATOR* )
            printf "${line}\n" >> gmo
          ;;
         *AIRTEL-NETWORK* )
            printf "${line}\n" >> air
          ;;
         *VODAFONE-NETWORK* )
            printf "${line}\n" >> voda
          ;;
         * )
            printf "${line}\n" >> all_others 
          ;;
        esac
      done
done  

Note that in the case statement, you need to escape any spaces in match targets, i.e. *GLOBAL\ MOBILE\ OPERATOR*.

Also, I don't have a way to test this right now, so hopefully there are not too many syntax errors :-). If you do find problems, let's try and setup a chat.

And, if you are sure whois produces only one line of output per request, you probably dont't need the while loop. whois $ip | read line ; case "${line}" .... might work.

Actually managing the constantly increasing files, air, voda, gmo may require some thought on your part.

I hope this helps.

Upvotes: 1

Related Questions