Beispiel #1
0
 def sendall(self, msg, ip, port=9000, close=True):
     try:
         sock = socket.socket()
         sock.connect((ip, port))
         sock.sendall(utils.dumps_json(msg).encode() + b'\r\n\r\n')
         if close: utils.close_connection(sock)
         return sock
     except (TimeoutError, OSError): pass
Beispiel #2
0
def update_rules():
    global gSetupRules
    for bgp_router in Configuration.bgp_routers.keys():
        tn = utils.create_connection(bgp_router)
        deploy_rule(tn, "route-map Path-End-Validation permit 1")
        for rule_id in gSetupRules:
            deploy_rule(tn, "match ip as-path " + rule_id)
        deploy_rule(tn, "match ip as-path allow-all")
        deploy_rule(tn, "exit")
        utils.close_connection(tn)
Beispiel #3
0
def update_rules():
        global gSetupRules        
	for bgp_router in Configuration.bgp_routers.keys():
		tn = utils.create_connection(bgp_router)
		deploy_rule(tn, "route-map Path-End-Validation permit 1")
		for rule_id in gSetupRules:
                        deploy_rule(tn, "match ip as-path " + rule_id)
                deploy_rule(tn, "match ip as-path allow-all")
		deploy_rule(tn, "exit")
		utils.close_connection(tn)
Beispiel #4
0
def deploy_record(raw_record):
        record = raw_record.get()
	rule_id = "as" + str(record.asn)
	record_rule_ids[rule_id] = time.time()
	for bgp_router in Configuration.bgp_routers.keys():
		tn = utils.create_connection(bgp_router)
		deploy_rule(tn, "ip as-path access-list " + rule_id + " deny " + "_[^" + encode_neighbors(record.links) + "]_" + str(record.asn) + "_")
		if not record.transient_flag:
                        deploy_rule(tn, "ip as-path access-list " + rule_id + " deny _" + str(record.asn) + "_[0-9]+_")
		deploy_rule(tn, "exit")
		utils.close_connection(tn)
Beispiel #5
0
def read_configuration(bgp_router):
    if (Configuration.DEBUG):
        raw_cfg = file("BGP_config_example.txt").read()
        ip = json.loads(urllib.urlopen("http://ip.jsontest.com/").read())["ip"]
        raw_cfg = raw_cfg.replace("1.1.1.1", ip)
        return raw_cfg
    tn = utils.create_connection(bgp_router)
    tn.write("show running-config\n")
    config = tn.read(1024 * 1024)
    tn.write("exit\n")
    utils.close_connection(tn)
    return config
Beispiel #6
0
def read_configuration(bgp_router):
    if (Configuration.DEBUG):
        raw_cfg = file("BGP_config_example.txt").read()
        ip = json.loads(urllib.urlopen("http://ip.jsontest.com/").read())["ip"]
        raw_cfg = raw_cfg.replace("1.1.1.1",ip)
        return raw_cfg
    tn = utils.create_connection(bgp_router)
    tn.write("show running-config\n")
    config = tn.read(1024 * 1024)
    tn.write("exit\n")
    utils.close_connection(tn)
    return config
Beispiel #7
0
def deploy_record(raw_record):
    record = raw_record.get()
    rule_id = "as" + str(record.asn)
    record_rule_ids[rule_id] = time.time()
    for bgp_router in Configuration.bgp_routers.keys():
        tn = utils.create_connection(bgp_router)
        deploy_rule(
            tn, "ip as-path access-list " + rule_id + " deny " + "_[^" +
            encode_neighbors(record.links) + "]_" + str(record.asn) + "_")
        if not record.transient_flag:
            deploy_rule(
                tn, "ip as-path access-list " + rule_id + " deny _" +
                str(record.asn) + "_[0-9]+_")
        deploy_rule(tn, "exit")
        utils.close_connection(tn)
Beispiel #8
0
                                           tr_style=mt.tr_style,
                                           td_style=mt.td_style,
                                           )
                    )
        for i, sp in enumerate(sorted(sps)):
            body.append('<tr>')
            body.append(mt.td.format(data=i + 1, **css))
            body.append(mt.td.format(data=sp['code'], **css))
            body.append(mt.td.format(data=sp['name'], **css))
            body.append(mt.td.format(data=sp['min_store'], **css))
            body.append(mt.td.format(data=sp['max_store'], **css))
            body.append(mt.td.format(data=sp['store_good'], **css))
            body.append(mt.td.format(data=sp['store_bad'], **css))
            body.append(mt.td.format(data=sp['out_good'], **css))
            body.append(mt.td.format(data=sp['out_bad'], **css))
            body.append('</tr>')

        body.append(mt.footer)

        body.append('<br /><br />')
        body.append(line)
        body.append('<br />')
        body.append(remark)
        send_mail(subject=subject,
                  body=''.join(body),
                  author=author,
                  to=to_list,
                  )

utils.close_connection(conn)
Beispiel #9
0
 def close_spider(self, spider):
     close_connection(self.db)
Beispiel #10
0
def main():
    parser = argparse.ArgumentParser(description='DevNet Tool')
    parser.add_argument('--user', type=str, required=True, help='Username')
    parser.add_argument('--password', type=str, required=True, help='Password')
    parser.add_argument('--network',
                        type=str,
                        required=True,
                        help='Management Network')
    parser.add_argument('--ntp-server',
                        type=str,
                        required=True,
                        help='NTP server')

    args = parser.parse_args()

    validate_args(args)

    output_report = ''

    for address in get_ip_address_list(args.network):
        print(f'connection started to {address}')

        connection = make_connection(address=address,
                                     user=args.user,
                                     password=args.password)
        if not connection:
            continue
        print('connection success')

        connection.enable()

        device_info = parse_show_version(connection=connection)
        if bool(device_info) is False:
            print('show version parse failed, skip')
            continue

        config = make_backup(connection=connection,
                             hostname=device_info['Hostname'])
        if not config:
            print('failed to get config, skip')
            continue

        device_cdp_info = parse_cdp_neighbor_detail(
            connection=connection, hostname=device_info['Hostname'])

        config_timezone_ntp(connection=connection,
                            hostname=device_info['Hostname'],
                            ntp_server=args.ntp_server,
                            config=config)

        ntp_service_info = parse_ntp_status(connection=connection,
                                            hostname=device_info['Hostname'])

        close_connection(connection=connection)

        output_report += make_report(device_info=device_info,
                                     cdp_info=device_cdp_info,
                                     ntp_info=ntp_service_info)

    if output_report:
        print('----SUMMARY----\n')
        print(output_report)
Beispiel #11
0
    def lookup_value(self, ID):
        to_query = self.node.get_n_closest(ID, self.node.alpha)

        pending = []
        heapq.heapify(pending)

        myinfo = (self.node.ID ^ ID, self.node.asTuple())
        enquired = [myinfo]
        alives = [myinfo]

        if len(to_query) == 0:
            founded, data, file_bytes = self.node.FIND_VALUE(ID)
            if founded: return (True, data, file_bytes)
            else: return (False, myinfo, None)
        else:
            founded, data, file_bytes = self.node.FIND_VALUE(ID)
            if founded: return (True, data, file_bytes)

        closest_node = heapq.nsmallest(1, to_query)[0][0]

        round = 1
        while True:

            for d,n in to_query:
                k_closest, data = [], None
                if not Node.Equals(n, self.node):
                    msg = utils.build_FIND_VALUE_msg(ID, self.node.asTuple())

                    sock = self.sendall(msg, n[1], close=False)
                    # data = self.get_response(msg['key'])
                    data = self.recvall(sock)
                    utils.close_connection(sock)

                    if data != None:
                        data = json.loads(data)
                        if data['result'][0]: return data['result']
                        # if data['result'][0]: return data
                        k_closest = [(t[0], tuple(t[1])) for t in data['result'][1]]
                        self.update(tuple(data['sender']))

                if (d,n) not in enquired:
                    enquired.append((d, n))

                if data != None and (d,n) not in alives:
                    alives.append((d, n))

                for t in k_closest:
                    if not t in enquired and not t in to_query and not t in pending:
                        pending.append(t)

            to_query.clear()

            if pending == []:
                break

            c = heapq.nsmallest(1, pending)[0][0]
            top = self.node.alpha if c <= closest_node else self.node.k
            closest_node = min(closest_node, c)
            for _ in range(top):
                try:
                    to_query.append(heapq.heappop(pending))
                except:
                    break
            round += 1

        return (False, heapq.nsmallest(self.node.k, enquired), None)
Beispiel #12
0
    def lookup_node(self, ID):
        ID = int(ID)
        to_query = self.node.get_n_closest(ID, self.node.alpha)

        pending = []
        heapq.heapify(pending)

        myinfo = (self.node.ID ^ ID, self.node.asTuple())
        enquired = [myinfo]
        alives = [myinfo]

        if len(to_query) == 0:
            return [(self.node.ID ^ ID, self.node.asTuple())]
            # return []

        closest_node = heapq.nsmallest(1, to_query)[0][0]

        round = 1
        while True:

            for d,n in to_query:
                k_closest, data = [], None
                if not Node.Equals(n, self.node):

                    # k_closest = n[1].FIND_NODE(ID, self.node.k)
                    msg = utils.build_FIND_NODE_msg(ID, self.node.asTuple())

                    ip, port = n[1], n[2]
                    # self.send_udp_msg(utils.dumps_json(msg).encode(), (ip, port))
                    # # data = self.get_answer(msg['key'])
                    # data = self.get_response(msg['key'])

                    sock = self.sendall(msg, n[1], close=False)
                    # data = self.get_response(msg['key'])
                    data = self.recvall(sock)
                    try:
                        data = json.loads(data)
                    except: data = None
                    utils.close_connection(sock)

                    if data:
                        k_closest = [(t[0], tuple(t[1])) for t in data['result']]
                        self.update(tuple(data['sender']))

                if (d,n) not in enquired:
                    enquired.append((d, n))

                if data != None and (d,n) not in alives:
                    alives.append((d, n))

                for t in k_closest:
                    if not t in enquired and not t in to_query and not t in pending:
                        pending.append(t)

            to_query.clear()

            if pending == []:
                break

            c = heapq.nsmallest(1, pending)[0][0]
           # print(c)
            #print(closest_node)
            top = self.node.alpha if c <= closest_node else self.node.k
            closest_node = min(closest_node, c)
            for _ in range(top):
                try:
                    to_query.append(heapq.heappop(pending))
                except:
                    break
            round += 1

        # return heapq.nsmallest(self.node.k, enquired)
        return heapq.nsmallest(self.node.k, alives)