Exemplo n.º 1
0
 def __init__(self, bind_ip, bind_port):
     threading.Thread.__init__(self)
     self.bind_ip = bind_ip
     self.bind_port = bind_port
     self.sock = None
     self.stop_flag = False
     self.device = []
     self.daemon = True
     self.mongo = get_mongodb()
Exemplo n.º 2
0
    def __init__(self, device_info, ssh_info, snmp_info):
        self.ip = device_info['ip']
        self.ssh_info = ssh_info
        self.ssh_info['ip'] = self.ip
        self.snmp_info = snmp_info
        self.snmp_community = snmp_info['community']
        self.snmp_port = snmp_info['port']
        self.type = 'device'

        # Default status
        self.status = self.STATUS_OFFLINE

        self.info = {}

        self.neighbor = []
        self.route = []
        self.subnets = {}

        self.mongo = get_mongodb()
Exemplo n.º 3
0
    def __init__(self, netflow_ip='127.0.0.1', netflow_port=23456):
        self.__create_time = time.time()
        self.devices = []
        self.subnets = []
        self.app_service = services.AppService()

        self._snmp_worker = SNMPWorker()

        self._netflow_worker = NetflowWorker(netflow_ip, netflow_port)

        # self.path = FindPath()

        self.mongo = get_mongodb()

        self.init()

        self.device_service = services.DeviceService()
        # self.graph, self.matrix = self.create_graph()
        logging.info("Create topology")
 def __init__(self):
     self.mongodb = get_mongodb()
     self.db = self.mongodb.db
Exemplo n.º 5
0
from flask import request, Flask
import redis
from flask import render_template
import time
from database import get_mongodb
import settings

app = Flask(__name__)
r = redis.StrictRedis()
db = get_mongodb(settings)


@app.route('/push', methods=['GET'])
def record():
    ip = request.remote_addr
    index = request.args.get('index')
    if index:
        r.hset('proxy', index, ip)
        update()
        return "push success"
    else:
        return "push faild"


def update():
    """更新squid反代节点"""

    ip_list = r.hgetall('proxy')
    with open('/etc/squid/squid.conf.example') as file:
        text = file.read()
        for i, ip in ip_list.items():
Exemplo n.º 6
0
def create_graph(devices):
    """
    """
    num_device = len(devices)

    matrix = []
    graph = {}
    conn = get_mongodb()
    for n1 in range(num_device):
        # Resetting neighbor
        devices[n1].neighbor = []
        device_1_name = devices[n1].get_name()
        _matrix = {'name': device_1_name, 'connected': []}
        graph[devices[n1]] = []

        # Check CDP Enable
        cdp = devices[n1].get_cdp()
        if cdp.get('neighbor'):
            for neighbor in cdp.get('neighbor'):
                neighbor_device = conn.device.find_one(
                    {'interfaces.ipv4_address': neighbor.get('ip_addr')})
                if neighbor_device is None:
                    continue
                if_index = -1
                for neighbor_interface in neighbor_device.get('interfaces'):
                    # Default is -1, -2 because if no IP is not match
                    if neighbor_interface.get('ipv4_address',
                                              -1) == neighbor.get(
                                                  'ip_addr', -2):
                        if_index = neighbor_interface['index']
                        break
                neighbor_info = {
                    "neighbor_ip": neighbor['ip_addr'],
                    # "neighbor_if_index": n4,
                    "neighbor_obj": neighbor_device,
                    "ip": devices[n1].ip,
                    "if_index": if_index,
                    "port": neighbor['port']
                }
                devices[n1].neighbor.append(neighbor_info)
                for device in devices:
                    if device.ip == neighbor_device.get('device_ip'):
                        graph[devices[n1]].append(device)
                        break
            continue
        # If CDP Not enable use SNMP
        for n2 in range(num_device):
            device_2_name = devices[n2].get_name()
            # device_2_info = devices[n1].get_info()
            # _d2_matrix = {
            #     'name': device_2_name,
            #     'connected': False
            # }
            if n1 == n2:
                # _d2_matrix['connected'] = True
                # _matrix['connected'].append(_d2_matrix)
                continue

            device_1_if = devices[n1].get_interfaces()
            device_2_if = devices[n2].get_interfaces()
            for n3 in range(len(device_1_if)):
                stop_flag = False
                d1_ip = device_1_if[n3].get('ipv4_address')
                d1_subnet = device_1_if[n3].get('subnet')
                if not d1_ip:
                    continue
                d1_ip_network = ipcalc.Network(d1_ip, d1_subnet)

                ### TEST
                # if d1_ip in ipcalc.Network('192.168.106.0', '255.255.255.0'):
                #     continue
                ### END TEST

                for n4 in range(len(device_2_if)):
                    d2_ip = device_2_if[n4].get('ipv4_address')
                    d2_subnet = device_2_if[n4].get('subnet')
                    if not d2_ip:
                        continue
                    if d2_ip in d1_ip_network:
                        graph[devices[n1]].append(devices[n2])
                        # Add neighbor to Device object
                        neighbor_info = {
                            "neighbor_ip": d2_ip,
                            # "neighbor_if_index": n4,
                            "neighbor_obj": devices[n2],
                            "ip": d1_ip,
                            "if_index": n3
                        }
                        devices[n1].neighbor.append(neighbor_info)
                        stop_flag = True
                        break
                # if stop_flag:
                #     _d2_matrix['connected'] = True
                #     break
        #     _matrix['connected'].append(_d2_matrix)
        # matrix.append(_matrix)
    logging.debug(graph)
    topo_graph = Graph(graph)
    # print(matrix)
    return topo_graph
Exemplo n.º 7
0
import time
import requests
import settings
from database import get_mongodb
from threading import Thread
from bs4 import BeautifulSoup
import logging


db = get_mongodb()
logging.basicConfig(level=logging.INFO,
                format='%(asctime)s %(levelname)s %(message)s',
                datefmt='%Y-%m-%d %H:%M:%S',
                    )


def get_data(item):

    new_item = dict.copy(item)
    url = item['link']
    logging.info("start check {}".format(url))
    try:
        response = requests.get(url, timeout=30)
        soup = BeautifulSoup(response.text, 'lxml')
        title = soup.find('title')

        if title:
            new_item['title'] = title.text
        if response.status_code != 200 or '已被删除' in response.text:
            new_item['status'] = False
        else:
Exemplo n.º 8
0
 def __init__(self, *args, **kwargs):
     self.mongodb = get_mongodb()
     self.db = self.mongodb.db
Exemplo n.º 9
0
from netaddr import *
import database
import pprint

mongo = database.get_mongodb()

if __name__ == '__main__':
    routes = mongo.route.find()
    # pprint.pprint(routes.count())
    for route in routes:
        ip = IPNetwork("{}/{}".format(route['dest'], route['mask']))

        if ip.size == 1:
            start_ip = ip.first
            end_ip = ip.first
        elif ip.size == 2:
            start_ip = ip.first
            end_ip = ip.last
        else:
            start_ip = ip.first + 1
            end_ip = ip.last - 1

        # print("Start: {}, End: {}".format(start_ip, end_ip))
        mongo.route.update_one(
            {'_id': route['_id']},
            {'$set': {
                'start_ip': start_ip,
                'end_ip': end_ip
            }})
        print("Route {} - {} is update".format(route['dest'], route['mask']))
from database import get_mongodb

mongo = get_mongodb()


def main():
    device_db = mongo.device
    for change in device_db.watch():
        print(change)


if __name__ == '__main__':
    main()
Exemplo n.º 11
0
 def __init__(self, flow_name):
     mongo = get_mongodb()
     flow = mongo.flow_table.find_one({'name': flow_name})
Exemplo n.º 12
0
 def fork(self):
     self.mongo = get_mongodb()