[Israel.pm] Renaming folder using perl

Berler Chanan bc.other at gmail.com
Tue May 5 08:17:56 PDT 2009


Hi All,


I will explain the idea, but first I wanted to say I liked the doc joke
alllloooooot

Preview:
Nagios runs command using agents found on hosts. Each agent has a plugins
folder where
all plugins are found and are being run, and report back to Nagios.

My Idea:
I need to build a nagios plugin / agent manager.
Currently I build a plugin manager that uses LWP to download the plugin,
update the nrpe.cfg / NSC.ini
file and restart the Agent Service. This command like all others is being
run from plugins folder (NSClient++ folder if I must be correct);
Until here there is no need to change any of the folders names.

Now I was asked to build an agent manager, that will download (again with
LWP) the agent, zipped.
Then moves / rename the nagios / NSClient++ folder to backup_<scalar
localtime>
Unzip the new agent into the new folder (which has the same name), and then
restart the nagios agent service.

For that I need to rename the folder which is the parent of the current
folder.

Thanks
Chanan

PS: I tested something that helped me figure if I move the folder's name to
another name, it won't effect he plugin from running.
Here is my test:

I set a for loop counting from 1 to 10000000, but before the loops starts I
unlinked the script.
I found that although the script is still running, the script itself no
longer exists.
Therefore I assumed that if I change the folder name to another name, the
script will still be running, while
It will be reserved in another place.



-----Original Message-----
From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il] On Behalf
Of sawyer x
Sent: Tuesday, May 05, 2009 3:46 PM
To: Perl in Israel
Subject: Re: [Israel.pm] Renaming folder using perl

Hi

> I need to write a script that renames the folder the script is running
from:
> The idea to rename the current folder to a back_<localtime>, then download
a
> zip file and extract it

This seems like something that should fail regardless of the rename.
It's insecure, can cause FS problems and doesn't make a lot of sense.
Why does the script have to be run from within a folder that needs to
be renamed? In UNIX (I'm not sure how it is with Windows), there is no
"renaming" folders function. It doesn't exist. You "mv" stuff. That
doesn't just rename it, it gives it a different inode and everything.
That means that - to the OS and FS - it's not the same folder. This
detaches the script's environment, making it much easier to
compromise. Beyond that, you can have serious errors trying to change
or fetch environment variables. When running with FS functions, it
usually calls environment variables or uses the environment in some
way which a good point to target when trying to compromise the script.

> (the main Idea to replace the current agent with another nagios agent).

This shouldn't be run from the same folder that is being moved.

> Problem: how can I rename the current folder the script is running from ?
> trying rename function fails (like system rename).

There is no original "rename" function in UNIX. There is a "rename"
utility Larry Wall wrote, that helps you change files. Sort of like
perl -pi -e.
If you want to use UNIX system commands, there's "mv" - as I've stated
above. I think you better check modules that provide portability:
File::Rename, File::PerlMove. I'm not sure if they support folders,
since it's a bit trickier.

My advice on your act is:
- Don't ever change the environment of a script like that
- Run the script from outside
- Do an atomic operation with rename so it wouldn't be a problem (such
as File::Transaction::Atomic, File::AtomicWrite)


Also, you should use File::Spec in order to avoid knowing whether to
use "../" or "..\" - not to mention Mac OS paths.

Sawyer.
_______________________________________________
Perl mailing list
Perl at perl.org.il
http://mail.perl.org.il/mailman/listinfo/perl
No virus found in this incoming message.
Checked by AVG - www.avg.com 
Version: 8.0.238 / Virus Database: 270.12.4/2081 - Release Date: 05/04/09
17:51:00



More information about the Perl mailing list