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

I have a current app using win32::ChangeNotify to monitor a directory and its sub-directories for files arriving via ftp.

I need to port this app to unix. Is there a generic Perl equivalent command to do this? or does someone have a snippet of code thay can point me to that will give me a start on this?

Thanks

Edit by tye, preserve formatting, add to title

  • Comment on win32::ChangeNotify replacement for Unix?

Replies are listed 'Best First'.
Re: win32::ChangeNotify replacement for Unix?
by tachyon (Chancellor) on Feb 17, 2004 at 20:47 UTC

    Essentially what you want to do is quite similar to basic intrusion monitoring. While you could easily configure tripwire to do this that would be total overkill. What you probably want is a basic IDS like Fcheck which is in perl so you can hack the good bits out.

    If you are just monitoring a single dir you could just have a quick script that runs under cron. Make it silent for no changes and run it under your cron tab and it will email you with every change.

    [root@devel3 root]# cat test.pl #!/usr/bin/perl my $dat = '/tmp/dat'; my $dir = '/root/'; if ( -e $dat ) { `ls -al $dir > $dat.current`; if ( `cat $dat` eq `cat $dat.current` ) { print "Dir $dir probably unchanged!\n"; } else { print "Dir $dir has changed\n", `diff -b $dat $dat.current`; `cat $dat.current > $dat`; } } else { `ls -al $dir > $dat`; # first pass init } [root@devel3 root]# ./test.pl # init stage [root@devel3 root]# ./test.pl Dir /root/ probably unchanged! [root@devel3 root]# touch foo [root@devel3 root]# ./test.pl Dir /root/ has changed 22a22 > -rw-r--r-- 1 root root 0 Feb 17 20:57 foo [root@devel3 root]# touch test.pl [root@devel3 root]# ./test.pl Dir /root/ has changed 56c56 < -rwxr-xr-x 1 root root 385 Feb 17 20:57 test.pl --- > -rwxr-xr-x 1 root root 385 Feb 17 20:58 test.pl [root@devel3 root]#

    If you just wanted a list of filenames that has changed then just use ls. With ls -al it will pick changes to perms, ownership and M time of existing files as well as deletions/additions.

    cheers

    tachyon

Re: win32::ChangeNotify replacement for Unix?
by Anonymous Monk on Feb 17, 2004 at 21:00 UTC
    Linux provides a dnotify system call that can handle the task of notifying your program when a change is made; I don't know if there exists a perl interface to it, however.
Re: win32::ChangeNotify
by Grygonos (Chaplain) on Feb 17, 2004 at 20:22 UTC

    You could keep a data structure that represents the directory structure, and recursively run through that structure looking for changes, if you find one .. write it to a log or notify the proper processes/authorities and add it to the data stucture. I believe recursion along with the perl functions opendir readdir closedir will provide you with what you need.

    Here is a function I use to log the directory structure of a give root node... keep in mind I'm not keeping track of it in the data structure method that I suggested, I'm simply writing it to a log.

    Hope some of that helps you get on your way to your solution.

    Grygonos