Need to scan entire server for IP and replace it

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
I'm trying to figure out the best way to script a command to search all text files on a server for a string (IP address) and replace it.

The closest I've gotten is this... but its not working

grep -rl '192.168.111' ./ | xargs sed -i 's/192.168.111/192.168.121/g'

Basically, I want to find instances of '192.168.111' and replace it with '192.168.121'

Thoughts?
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
I would use 'find' to help you locate the files you need. You can use it similar to xargs, in that you can give it a command to run for all the files it finds etc.
 

manly

Lifer
Jan 25, 2000
12,918
3,687
136
Remember to view the list of matching files before actually modifying them; backing them up would be advisable. Don't modify any log files.
 

Red Squirrel

No Lifer
May 24, 2003
69,807
13,379
126
www.anyf.ca
Are these places that could accept a DNS name? Would be easier to replace it with that, so next time you just have to change it in one place. If it's stuff that requires an IP, how many places do you have to change it? It may be faster to do it manually than to write a script to do it. Just a thought.
 

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
Are these places that could accept a DNS name? Would be easier to replace it with that, so next time you just have to change it in one place. If it's stuff that requires an IP, how many places do you have to change it? It may be faster to do it manually than to write a script to do it. Just a thought.

Nope, it's mostly network configuration and stuff like that. I don't want to add a dns layer to it.

I've elected to just keep a list of the files that need fixin, and do direct replaces on those specific files. There's only 4-7 files so it's not worth scanning everything and potentially replacing wrong things.
 

Scarpozzi

Lifer
Jun 13, 2000
26,391
1,780
126
Do you not know what services are running on the server?

Start by running netstat and see what's listening. Write down the list of address/port combinations....then cross reference that with running daemons/services. Once you get those outlined, you can seek the config files and properly change it.

It's not a good idea to use sed in said way.
 

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
Do you not know what services are running on the server?

Start by running netstat and see what's listening. Write down the list of address/port combinations....then cross reference that with running daemons/services. Once you get those outlined, you can seek the config files and properly change it.

It's not a good idea to use sed in said way.

These are small, minimal CentOS installs with dedicated purposes. I know exactly what's running on the server.

What needs to be changed are configuration files. My desire to scan everything and change things that way was laziness.

I'm just going to do a replace on the config files directly. Easier to manage.
 

Scarpozzi

Lifer
Jun 13, 2000
26,391
1,780
126
These are small, minimal CentOS installs with dedicated purposes. I know exactly what's running on the server.

What needs to be changed are configuration files. My desire to scan everything and change things that way was laziness.

I'm just going to do a replace on the config files directly. Easier to manage.
Definitely if you're talking about a lot of systems and are able to save a copy off those boxes and maintain them in one location. (makes for quick and easy reinstalls too)

I've used sed for a lot of config file rewrites. The way I handle it is by taking the configuration files and creating config file templates....then for each config string in the template I edit, I'll give it a deliberately long variable name.

So it might say "Listen 192.168.1.230". I'll save the template with "IPADDRLISTENCOMMAND" or something like that. Then have sed go in and replace instances of "IPADDRLISTENCOMMAND" with "Listen 192.168.1.210" or whatever....
 

mv2devnull

Golden Member
Apr 13, 2010
1,519
154
106
There cannot be many files, because even a server could use DHCP to get an IP, and dhclient cannot possibly edit en masse of configuration files.

Start with:
/etc/sysconfig/network-scripts/ifcfg-*
/etc/hosts


PS. Kickstart installation is really convenient.
 

Scarpozzi

Lifer
Jun 13, 2000
26,391
1,780
126
There cannot be many files, because even a server could use DHCP to get an IP, and dhclient cannot possibly edit en masse of configuration files.

Start with:
/etc/sysconfig/network-scripts/ifcfg-*
/etc/hosts


PS. Kickstart installation is really convenient.

It's bad practice to use /etc/hosts unless doing layer-4 switching or something that would make DNS unreliable.