Remotely Watch Windows File System For Changes

Questions about programming languages and debugging
Post Reply
User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Remotely Watch Windows File System For Changes

Post by maboroshi »

### REQUIRED ###

In order to make this work via source code
Download Stackless Python 2.5.2 www.stackless.com
Download win32 by Mark hammond http://python.net/crew/mhammond/
Should then work running python NetworkOSwatching.py from CMD

### RUNNING FROM BINARY ###
In order to run this from Binary
Please make sure you have Windows Vista with SP1 Some problems have been reported running on XP SP2.

If you would like to compile a binary for XP that would be appreciated to ;)

### END REQUIRED ###

This is a server written in Python

[1] Connect via Putty on target computer using Raw connection port 8888. Example "127.0.0.1" 8888 Raw
[2] After successful connection enter command "START" With out the quotes
[3] Watch for recursive file system changes

This app uses stackless for threads

Code: Select all

www.stackless.com
Not sure if my stackless code is implemented correctly but it seems to work

Here is a binary and source download...

Code: Select all

http://www.techshinobi.com/NetworkOSWatching.zip
To change top level directory modify

Code: Select all

path_to_watch = "C:\\"
to anything you choose

example

Code: Select all

path_to_watch = os.getcwd()

Code: Select all

import os
import sys
import time
import win32file
import win32event
import win32con

import socket, traceback
import stackless

host = ""
port = 8888

def handlechild():
    print "Got connection from", clientsock.getpeername()
    while 1:
        data = clientsock.recv(4096)
        if data == "START":
            watchos()
        if not len(data):
            break
        clientsock.sendall(data)


    clientsock.close()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

s.bind((host, port))
s.listen(10)

# This Function taken and modified from
# http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html
# All credit to him

def watchos():
    #get path or maintain current path of app
    FILE_LIST_DIRECTORY = 0x0001
    path_to_watch = "C:\\"
    path_to_watch = os.path.abspath(path_to_watch)

    clientsock.send("Watching %s at %s" % (path_to_watch, time.asctime()))
    # FindFirstChangeNotification sets up a handle for watching
    #  file changes.
    while 1:
        hDir = win32file.CreateFile (
            path_to_watch,
            FILE_LIST_DIRECTORY,
            win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
            None,
            win32con.OPEN_EXISTING,
            win32con.FILE_FLAG_BACKUP_SEMANTICS,
            None
            )

        change_handle = win32file.ReadDirectoryChangesW (
            hDir,
            1024,
            True,#Heap Size include_subdirectories,
            win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
            win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
            win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
            win32con.FILE_NOTIFY_CHANGE_SIZE |
            win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
            win32con.FILE_NOTIFY_CHANGE_SECURITY,
            None,
            None
            )

        # Loop forever, listing any file changes. The WaitFor... will
        #  time out every half a second allowing for keyboard interrupts
        #  to terminate the loop.
        ACTIONS = {
            1 : "Created",
            2 : "Deleted",
            3 : "Updated",
            4 : "Renamed from something",
            5 : "Renamed to something"
            }
        results = change_handle
        for action, files in results:
            full_filename = os.path.join(path_to_watch, files)
            theact = ACTIONS.get(action, "Unknown")
            clientsock.send("\n" + str(full_filename) + str(theact) +"\n")

while 1:
    try:
        clientsock, clientaddr = s.accept()
    except KeyboardInterrupt:
        raise
    except:
        traceback.print_exc()
        continue

    channel = stackless.channel()
    stackless.tasklet(handlechild)()
    stackless.run()
Last edited by maboroshi on 05 May 2009, 10:12, edited 3 times in total.

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

I haven't gone through the code very much, because I want the answers from you :) (mostly because I can't be sure if my interpretation is 100% correct)

It watches a directory for any changes right? Files in the directory and other directories inside of it? But does it do this recursively, as in watch subfolders and subfolders of the subfolders, etc? =)

Would come in real handy if it does :-k
"The best place to hide a tree, is in a forest"

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Yes it does

Post by maboroshi »

Yes it does just change this in the watchos function

path_to_watch = os.getcwd()

to the main top level directory

example

path_to_watch = "C:\\"

Please note I am using stackless in this application for threading I am not 100 % it is implemented correctly but it seems to work so I won't complain

also to execute the function you will need to type in "START" in Putty without quotes

please note the formatting is imbalanced to.....

here is a pic using recursive from C:\


Image


Cats I would like to see a C++ Version ASAP Chop Chop *slaps cats with a trout

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

yes, I'll put it on my ToDo list, I'll have it ready in 4 years xD

But seriously, this IS on my ToDo list :D
"The best place to hide a tree, is in a forest"

Post Reply