Last active
March 26, 2018 23:28
-
-
Save Faxn/5afd54eed53af7087a70293e0cc0a18e to your computer and use it in GitHub Desktop.
Idea came up in conversation. It's a python script that starts a random program off your hard drive.
This file contains 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
import os.path | |
import sys | |
import random | |
import re | |
import argparse | |
from pathlib import Path | |
def main(fast=False, once=False): | |
if fast: | |
get_exe = random_walk | |
else: | |
exes = gather_walk() | |
get_exe = lambda: random.choice(exes) | |
while True: | |
file = get_exe() | |
print(file) | |
confirm = input("Good Idea?(y/N/q):") | |
if len(confirm) > 0 and confirm[0].upper() == 'Y': | |
pipe = os.popen(str(file)) | |
pipe.close() #This blocks until the program closes. | |
elif len(confirm) > 0 and confirm[0].upper() == 'Q': | |
break | |
elif once: | |
break | |
drives = None | |
def get_roots(): | |
if os.name == 'nt': | |
global drives | |
if drives == None: | |
drives = os.popen("fsutil fsinfo drives").readlines() | |
drives = ''.join(drives) | |
drives = list(re.findall('\s(\w:)', drives)) | |
return map(lambda x: x+'\\', drives) | |
else: | |
return ['/'] | |
def is_exec(file): | |
if os.name == 'nt': | |
if isinstance(file, Path): | |
return file.suffix == '.exe' | |
elif type(file) == str: | |
return re.search('\.exe$', file) | |
else: | |
return os.access(file, os.X_OK) | |
""" | |
Walks down the filesystem to find an executable at random. | |
it's biased toward files with paths with less steps in them. | |
or in smaller folders. | |
""" | |
def random_walk(file=None, tries=0): | |
if file == None: | |
file = get_root() | |
try: | |
if file.is_dir(): | |
di = [x for x in file.iterdir()] | |
if len(di) > 0: | |
fl = random.choice(di) | |
return random_walk(fl, tries) | |
elif is_exec(file): | |
return file | |
else: | |
pass | |
except PermissionError: | |
pass | |
#fallthrough, if we haven't retuned something went wrong. | |
#print("%d: %s" % (tries, file)) | |
return random_walk(None, tries+1) | |
def gather_walk(): | |
execs = [] | |
for root in get_roots(): | |
for dirpath, dirnames, filenames in os.walk(root): | |
#print(dirpath) | |
for file in filenames: | |
#print(dirpath+file) | |
if is_exec(file): | |
execs.append(dirpath+file) | |
if len(execs) % 1000 == 0: | |
print(len(execs)) | |
return execs | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='') | |
parser.add_argument('-o', '--once', action='store_true', help='Only run once instead of as a loop.') | |
parser.add_argument('-f', '--fast', action='store_true', help='Use a fast to chose, but biased selection method.') | |
args = parser.parse_args() | |
main(**vars(args)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment