globalsec23 has asked for the wisdom of the Perl Monks concerning the following question:

I have a program i'm working on that basically will automate a virtual machine post install + extending an lvm

i've got the code for the extending all working

now my clone server that will run this program has an ip and FQDN that i need to change

the program usage is as follows:

Usage: ./update-vm -i <IP_ADDRESS> -s <SERVER_NAME> -i [IP_ADDRESS] IP Address to change to, format: A.B.C.D -s [SERVER_NAME] Hostname to change to, format: foo.foo.com or f +oo.foo.foo.foo.com -h Show this text

i was trying to avoid using system or just one liner perl search/replaces for the IP and hostname strings to search for in /etc/ and change

my variables are $old_ip statically set in program, $old_name statically set in program, $new_ip gotten from ARGV and $new_name gotten from ARGV

I did read about Find::File and Find::Glob and File::Slurp but i cant find any really good examples that dont just try and do find or system calls which i would like to avoid for best practice reasons, plus the examples ive seen are calling from a file where im grabbing from user input from my Getops::Long

thanks in advance for your help monks!

if i can provide anymore information ill be happy to!

  • Comment on need to be able to search/replace by ip and hostname recursively in /etc based off ARG input
  • Download Code

Replies are listed 'Best First'.
Re: need to be able to search/replace by ip and hostname recursively in /etc based off ARG input
by arkturuz (Curate) on Jul 25, 2013 at 09:39 UTC
    If you need to use system(), then use it. It's done thousands of times per day on a typical machine. If your script is not run every second or so, then it's probably ok. On the other hand, changing potentially a lot of files in /etc might be a bad thing. What if you want to change it all back? Learn to use firewall rules (it could be a really simple solution with those) if you need to redirect traffic or something like that. I would suggest, if you really want to do this, to go simple like:
    perl -p -i -e 's/$old_ip/$new_ip/g' /etc/*files_to_change*
    Consult perlrun documentation for "-i" switch and backup options when changing a lot of files like this.

      Hi thanks for the reply, let me clarify a few points

      This program is designed to help speed up the deployment of provisioning new virtual machines based off a lamp stack clone with all our post install steps, so this wouldn't ever be undone and firewall rules are not relevant in the scenario

      It would be run once per server during the deployment process

      I was simply trying to not have to rely on system and learn a more best practices way of adjusting files in a search and replace scenario

      also the number of files to adjust is under 20

      hopefully tht clarifies my desires and the purpose better, thanks

        Yes, it's more clear now. I think a simple script I mentioned before would be enough for this. It works out of the box - opens every file you specify, replaces what you need to replace and, most important, is very short code.