Replacing text in several files in *nix (perl one-liner)
Scenario: lets say you have a server in a network and have several services running on it. Now, there is , say a iptables script running with your current ipmask (192.168.2.X) and say, a dns server with zone files that contain your current ip address range also. NOw your boss comes and says its been decided that you need to change your ip range . the first thing you do is go into shock because you know the workload of changing all ip addresses. Well here is a solution. Get your new IP range (for example, 10.1.2.X) and keep everyone’s ip the same, so that the machine with 192.168.2.1 gets 10.1.2.1 etc. Then go to your dns server and use the following command line to change ALL ipmasks to the new rangs in about 5 seconds flat:
# perl -pi.bak -e 's/192.168.2/10.1.2/g;' *.zone
Explanations:
“-pi.bak” makes a backupfile of each edited file before editing, with the ending “.bak”
“-e” apply an expression.
“‘ ‘” opens and closes and expression.
“s” means search and replace the following (which is in this case 192.168.2) with the next (in this case 10.1.2).
“g” mean globally, so in the whole file
“*.zone” deignates which files are affected, you can aslo just specify one file. In this case we chose all zone files from DNS. You may also replace it with something like /etc/* which would go thru all files in the /etc directory.
Like it? Do you have another solution that you want to share?
//Flosse
No TagsPopularity: 1% [?]
Where *nix and security meet the general public
Leave a comment
You must be logged in to post a comment.