Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
search&replace IP address in all config files?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
alexandero
n00b
n00b


Joined: 14 Jan 2004
Posts: 19
Location: Vienna, Austria

PostPosted: Fri Jul 02, 2004 9:31 am    Post subject: search&replace IP address in all config files? Reply with quote

Probably the subject explains my problem. I have to change to a new provider, so I will have to change nameservers, local ip address and broadcast. As Im running a web-& mailserver I set up in the past year, I think the IP address is stored in probably 10 different places.

To make sure I find everything, I'm looking for a way to search the complete filesystem (well, only text files, not binary) for these addresses. Any idea? Help appreciated...
Back to top
View user's profile Send private message
esoteriskdk
Tux's lil' helper
Tux's lil' helper


Joined: 15 Feb 2004
Posts: 92
Location: Denmark

PostPosted: Fri Jul 02, 2004 10:44 am    Post subject: Reply with quote

I'm no expert, but I'll give it a shot.

I don't know of a way to split binary files from text. But if you know in which directories the files you need to change are, you can use this command.
Code:
for FILE in *; do echo filename: $FILE && cat $FILE|grep YOUROLDIP; done

Change "YOUROLDIP" to the IP you want to look for. The above command will show you which files contains the IP you need to change.

You could also use "sed" to change everything automatically. But use with cation.
_________________
This nerd is using: 64Bit Gentoo Linux with 2.6.x | X.org | Fluxbox on a Dual Opteron 248
Back to top
View user's profile Send private message
mr-simon
Guru
Guru


Joined: 22 Nov 2002
Posts: 367
Location: Leamington Spa, Warks, UK

PostPosted: Fri Jul 02, 2004 10:44 am    Post subject: Reply with quote

How about something like:
Code:
#!/bin/bash

OLDIP="10.1.1.1"
NEWIP="192.168.1.1
DIR="/etc"

for i in `grep -Ilr "$OLDIP" $DIR/*`
do
  echo "Updating $i..."
  cp "$i" "$i.old"
  MATCH=`echo "$OLDIP" | sed 's/\./\\./g'`
  cat "$i.old" | sed "s/$MATCH/$NEWIP/g" > $i
done


Er, I haven't tried this or anything. I just made it up off the top of my head. I take no responsibility for any damage, etc. In theory, it *should* locate every non-binary file containing your old ip address, back it up to filename.old, and replace it with an updated version.
_________________
"Pokey, are you drunk on love?"
"Yes. Also whiskey. But mostly love... and whiskey."
Back to top
View user's profile Send private message
alexandero
n00b
n00b


Joined: 14 Jan 2004
Posts: 19
Location: Vienna, Austria

PostPosted: Fri Jul 02, 2004 11:18 am    Post subject: Reply with quote

Thanks for the help. As I don't really dare to use these scripts (as I am no programmer and couldn't tell if they happen to have a tiny mistake that deletes the files instead), could you point me out to the short version that only lists the files? I could then nano each one, I assume it won't be more than 10. thanks again.
Back to top
View user's profile Send private message
esoteriskdk
Tux's lil' helper
Tux's lil' helper


Joined: 15 Feb 2004
Posts: 92
Location: Denmark

PostPosted: Fri Jul 02, 2004 11:27 am    Post subject: Reply with quote

My script does just that. mr-simon even showed me how to improve it.

Code:
for FILE in `grep -Ilr "10.0.0.1" /etc/*` ; do echo Filename: $FILE && cat $FILE|grep 10.0.0.1 && echo -e '\n'; done

Change 10.0.0.1 to whatever the your old IP was.
The aboves command _does not_ change anything, it just shows you which filenames contains the IP your are looking for.

If you _only_ want the filenames that contain your old IP, then
Code:
for FILE in `grep -Ilr "10.0.0.2" /etc/*` ; do echo Filename: $FILE; done

I just ran both on my own box
_________________
This nerd is using: 64Bit Gentoo Linux with 2.6.x | X.org | Fluxbox on a Dual Opteron 248
Back to top
View user's profile Send private message
mr-simon
Guru
Guru


Joined: 22 Nov 2002
Posts: 367
Location: Leamington Spa, Warks, UK

PostPosted: Fri Jul 02, 2004 11:31 am    Post subject: Reply with quote

You don't need a script... Just use Grep:
Code:
grep -Ilr "your.i.p.address" /*

I'll talk you through the parameters:

grep <-- you should know what this is
-I <-- (capital i) treat binary files as non-matching... This way you only see textfiles that match
-l <-- (lowercase l) list filenames containing the string you searched for, instead of printing out the actual line that matches
-r <-- (lowercase r) search recursively through subdirectories
"your.i.p.address" <-- the string to search for
/* <-- the files to search for. Here, I've specified everything (*) in the root directory (/). You could similarly just say /etc/*
_________________
"Pokey, are you drunk on love?"
"Yes. Also whiskey. But mostly love... and whiskey."
Back to top
View user's profile Send private message
mr-simon
Guru
Guru


Joined: 22 Nov 2002
Posts: 367
Location: Leamington Spa, Warks, UK

PostPosted: Fri Jul 02, 2004 11:32 am    Post subject: Reply with quote

rats... beaten again! ;-)
_________________
"Pokey, are you drunk on love?"
"Yes. Also whiskey. But mostly love... and whiskey."
Back to top
View user's profile Send private message
alexandero
n00b
n00b


Joined: 14 Jan 2004
Posts: 19
Location: Vienna, Austria

PostPosted: Mon Jul 05, 2004 8:06 am    Post subject: Reply with quote

thanks for your help! It works...
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum