Created
April 20, 2016 03:23
-
-
Save pefoley2/e731dd33a8590d864d706efd23eff92c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 -OO | |
import logging | |
import socket | |
import pygame | |
import pyudev | |
import subprocess | |
import time | |
count = 0 | |
def main(): | |
soundfile = '/root/scripts/stolen.mp3' | |
pygame.init() | |
pygame.mixer.init() | |
pygame.mixer.music.load(soundfile) | |
context = pyudev.Context() | |
check_startup(context) | |
monitor = pyudev.Monitor.from_netlink(context) | |
monitor.filter_by('input') | |
observer = pyudev.MonitorObserver(monitor, callback=device_change) | |
observer.start() | |
observer.join() | |
def check_startup(context): | |
num = 0 | |
for dev in context.list_devices(SUBSYSTEM='input'): | |
if 'EV' in dev: | |
if 'ID_INPUT_MOUSE' in dev or 'ID_INPUT_KEYBOARD' in dev: | |
num = num + 1 | |
if num < 2: | |
noise() | |
def noise(): | |
devnull = open('/dev/null','w') | |
subprocess.call('amixer set Master 100%', stdout=devnull, shell=True) | |
subprocess.call('amixer set Speaker 100%', stdout=devnull, shell=True) | |
devnull.close() | |
if pygame.mixer.music.get_busy() == False: | |
pygame.mixer.music.play() | |
def device_change(dev): | |
global count | |
if 'EV' not in dev: | |
return | |
if dev.action == 'remove': | |
count += 1 | |
if dev.action == 'add': | |
count -= 1 | |
if count > 0: | |
if dev.action == 'remove': | |
log = open("/var/log/mouselog", "a") | |
text = "A input device was removed from {0} at {1}\nlogged in users are:\n".format(socket.gethostname(),time.asctime()) | |
log.write(text) | |
log.flush() | |
subprocess.call('who',stdout=log) | |
log.close() | |
noise() | |
else: | |
pygame.mixer.music.stop() | |
devnull = open('/dev/null','w') | |
subprocess.call('amixer set Master 0%', stdout=devnull, shell=True) | |
devnull.close() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment