__author__ = '@tomereyz'

from pwn import *


class I386Exploit(object):
    def __init__(self, challenge_name, execution, remote_port=20000):
        self.r = None
        if execution == 'debug':
            r = process(
                ['./{challenge_name}'.format(challenge_name=challenge_name)])
            gdb.attach(r, gdbscript=open('../gdb_plugin.sh'))
            log.info('xhelp - help in gdb')
        elif execution == 'local':
            r = process(['./{challenge_name}'.format(challenge_name=challenge_name)])
        elif execution == 'remote':
            ssh_client = ssh(user=challenge_name, host='exercises-i386.zenysec.com', port=remote_port,
                             password=challenge_name)
            r = ssh_client.process(['./{challenge_name}'.format(challenge_name=challenge_name)])
        else:
            log.error('Unknown execution type {execution}'.format(execution=execution))
            raise Exception('I386Exploit: Unknown execution type {execution}'.format(execution=execution))
        self.r = r
