View previous topic :: View next topic |
Author |
Message |
alexandero n00b
![n00b n00b](/images/ranks/rank_rect_0.gif)
Joined: 14 Jan 2004 Posts: 19 Location: Vienna, Austria
|
Posted: Fri Jul 02, 2004 9:31 am Post subject: search&replace IP address in all config files? |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
esoteriskdk Tux's lil' helper
![Tux's lil' helper Tux's lil' helper](/images/ranks/rank_rect_1.gif)
![](images/avatars/64075795440d0a282c672b.jpg)
Joined: 15 Feb 2004 Posts: 92 Location: Denmark
|
Posted: Fri Jul 02, 2004 10:44 am Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
mr-simon Guru
![Guru Guru](/images/ranks/rank_rect_3.gif)
![](images/avatars/9548866923e424191c72bf.gif)
Joined: 22 Nov 2002 Posts: 367 Location: Leamington Spa, Warks, UK
|
Posted: Fri Jul 02, 2004 10:44 am Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
alexandero n00b
![n00b n00b](/images/ranks/rank_rect_0.gif)
Joined: 14 Jan 2004 Posts: 19 Location: Vienna, Austria
|
Posted: Fri Jul 02, 2004 11:18 am Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
esoteriskdk Tux's lil' helper
![Tux's lil' helper Tux's lil' helper](/images/ranks/rank_rect_1.gif)
![](images/avatars/64075795440d0a282c672b.jpg)
Joined: 15 Feb 2004 Posts: 92 Location: Denmark
|
Posted: Fri Jul 02, 2004 11:27 am Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
mr-simon Guru
![Guru Guru](/images/ranks/rank_rect_3.gif)
![](images/avatars/9548866923e424191c72bf.gif)
Joined: 22 Nov 2002 Posts: 367 Location: Leamington Spa, Warks, UK
|
Posted: Fri Jul 02, 2004 11:31 am Post subject: |
|
|
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 |
|
![](templates/gentoo/images/spacer.gif) |
mr-simon Guru
![Guru Guru](/images/ranks/rank_rect_3.gif)
![](images/avatars/9548866923e424191c72bf.gif)
Joined: 22 Nov 2002 Posts: 367 Location: Leamington Spa, Warks, UK
|
Posted: Fri Jul 02, 2004 11:32 am Post subject: |
|
|
rats... beaten again! ![Wink ;-)](images/smiles/icon_wink.gif) _________________ "Pokey, are you drunk on love?"
"Yes. Also whiskey. But mostly love... and whiskey." |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
alexandero n00b
![n00b n00b](/images/ranks/rank_rect_0.gif)
Joined: 14 Jan 2004 Posts: 19 Location: Vienna, Austria
|
Posted: Mon Jul 05, 2004 8:06 am Post subject: |
|
|
thanks for your help! It works... |
|
Back to top |
|
![](templates/gentoo/images/spacer.gif) |
|