Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to grep for IP

Status
Not open for further replies.

sendhilk

IS-IT--Management
Mar 29, 2001
109
US


Hi,

I am going to change the IP address of my server. I am not sure about where all the IP has been hard coded in the scripts and the applications. I require help in writting a script that would scan all the files in the system for a particular IP and list all the files that hold the IP address.could anyone help me out with this script.

Thanks,
Sendhil




 
What tools do you have available? Perl? (Yes -- I know I write everything in Perl, but there you go) Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Hi Mike,

I don't have perl. could u please help me write it using normal shell script.
Sendhilk
 
Sendhilk,

Maybe, yes.

Your final goal is to end up with a script that will search your whole system - yes?

To get there we first need to write a script that will search each text file in a directory, for your string. We might call this script 'grep_text_files'

The second step is to use the 'find' program to search through each directory on your system.

Do I understand you correctly? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 


Yes Mike,

U got it perfectly right. I am working on the same lines too. could u please help me do that.

Thanks,
Sendhilk
 
Ok.

First step

Which are the text files in a directory?

You can get this with

file * | grep -i text

and then use the results of this to search for your string

grep your_string $(file * | grep -i text)

(I'm working without a UNIX system in front of me here (still!!!) so please forgive any typing mistakes)

This would be the basis of the script that will be called by the 'find' utility.

That line of code would go in the file 'grep_text_files' and that file marked as executable with the command 'chmod +x grep_text_files' -- with me so far?

#!/bin/ksh

grep $1 $(file * | grep -i text)
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 


I tried but it didnt work.
could u please explain in detail

Thanks,
Sendhil
 
You could do it using find, xargs and grep. Something like this (untested):

$ find / -type f | xargs grep -l '192\.192\.192\.1'

The -l flag to grep should just list the file names that contain the IP address, but check your man page as grep implementations vary across unices. Of course, you'll also want to replace the fictional address above with your machine's IP address.

Russ
bobbitts@hotmail.com
 
That looks very neat Russ -- how do you limit it to text files though? Grepping binary files is a low-calory affair at best. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Good point. Off the top of my head, I can't think of a really good way to do it. Here's something though:

$ find / -type f | xargs file | egrep -v '(ELF|executable)' | awk 'BEGIN { FS=":"; }{ print $1 }' | xargs grep -l '192\.192\.192\.1'

Of course, this all depends on file and its multitude of ways of describing files, so the argument to egrep -v would have to be fooled with a bit, depending on the implementation of file which makes this solution particularly lame :)

Russ
bobbitts@hotmail.com
 
Oh :-(

Thought you'd got a special trick up your sleeve. That was what I was trying to do the whole:

file * | grep -i text

business - to get a list of text files Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Yeah, what is better here? To exclude files based on strings that would suggest that they're binary files or include files based on strings that suggest text files? I'd go with the former myself even if it means that grep has to eat a few binary files.

Russ
bobbitts@hotmail.com
 
you could always grep -c, awk it to ignore the 0's and then 'file' the output :)

then the user could always decide which files to check :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top