Skip to content

Instantly share code, notes, and snippets.

@binkybear
Last active January 6, 2016 02:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binkybear/c0c46f94831f587a3779 to your computer and use it in GitHub Desktop.
Save binkybear/c0c46f94831f587a3779 to your computer and use it in GitHub Desktop.
filetohid.py
import argparse # Handle arguments
import os # To write hid comands to system
from keyseed import * # The bytes to translate to keyboard codes
'''
Arguments for filetohid.py
python filetohid.py -f [inputfile] -l us
python filetohid.py -s "this is a string" -l us
This is a modified version of duckhunter.py
'''
parser = argparse.ArgumentParser(description='Take input file or string and output it to hid')
parser.add_argument('-l', type=str, dest='layout', choices=['us', 'fr', 'de', 'es', 'sv', 'it', 'uk', 'ru', 'dk', 'no', 'pt', 'be'], help='Keyboard layout')
parser.add_argument('-f', '--file', type=str, help="Input file")
parser.add_argument('-s', '--string', type=str, help="Input string")
args = parser.parse_args()
# Variables for general keyboard commands, arguments
prefix = "echo "
suffix = " | hid-keyboard /dev/hidg0 keyboard"
input_string = args.string
filename = args.file
language = args.layout
# If no language is specified, default to English
if not language:
language = "us"
def do_file(filename, lang):
try:
f = open(filename, "r")
for line in f: # Read a line in the file
for char in line: # Read each character in that line
#
# Start conversion here
#
if char != '\n': # If the character is not a new line
if lang == "ru": # If russian, set characters to russian
char = iso_ru[char]
line = dicts[lang+'_bin'].get(char)
if line is not None:
if isinstance(line, str):
os.system('%s%s%s\n' % (prefix, line.rstrip('\n').strip(), suffix))
#print('%s%s%s\n' % (prefix, line.rstrip('\n').strip(), suffix))
else:
for elem in line:
os.system('%s%s%s\n' % (prefix, elem.rstrip('\n').strip(), suffix))
#print('%s%s%s\n' % (prefix, elem.rstrip('\n').strip(), suffix))
else:
line = dicts[lang][char]
os.system('%s%s%s\n' % (prefix, line.rstrip('\n').strip(), suffix))
os.system('echo -ne "\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00" > /dev/hidg0\n') # releases key
os.system('sleep 0.03 \n') # Slow things down
finally:
f.close()
def do_string(string, lang):
for char in string:
#
# Start conversion here
#
if char != '\n': # If the character is not a new line
if lang == "ru": # If russian, set characters to russian
char = iso_ru[char]
line = dicts[lang+'_bin'].get(char)
if line is not None:
if isinstance(line, str):
os.system('%s%s%s\n' % (prefix, line.rstrip('\n').strip(), suffix))
#print('%s%s%s\n' % (prefix, line.rstrip('\n').strip(), suffix))
else:
for elem in line:
os.system('%s%s%s\n' % (prefix, elem.rstrip('\n').strip(), suffix))
#print('%s%s%s\n' % (prefix, elem.rstrip('\n').strip(), suffix))
else:
line = dicts[lang][char]
os.system('%s%s%s\n' % (prefix, line.rstrip('\n').strip(), suffix))
os.system('echo -ne "\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00" > /dev/hidg0\n') # releases key
os.system('sleep 0.03 \n') # Slow things down
#
# If input file is passed with -f, take that input file and read it
#
if args.file:
do_file(filename, language)
#
# If string is passed with -s "this is a test string", print this to keyboard
#
if args.string:
do_string(input_string, language)
#
# Stop a user from using both string and file. Don't be crazy!
#
if args.string and args.file:
print("Select either -f or -s, not both!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment