Пример #1
0
def _get_hostname():
    client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT)
    resp = json.loads(client.get_information(["device_base_info"]))
    if resp["responseCode"] != 200:
        return ""
    else:
        device_info = resp["device_base_info"]
        return device_info["hostname"]
Пример #2
0
def _get_interface_info():
    client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT)
    resp = json.loads(client.get_information(["interfaces"]))
    if resp["responseCode"] != 200:
        return ""
    else:
        ret = "名称\tip地址\t\t状态\t模式\t速度\t双工模式\n"
        interfaces = resp["interfaces"]
        for interface in interfaces:
            eth = str(interface["device"])
            if (utils.get_device_type() == 1 or utils.get_device_type() == 5
                    or utils.get_device_type() == 11) and eth != "eth0":
                continue
            if eth == "eth0":
                ret += "Mgmt\t"
            elif eth == "eth1":
                ret += "MTA\t"
            elif eth == "eth2":
                ret += "P1\t"
            elif eth == "eth3":
                ret += "P2\t"
            elif eth == "br0":
                ret += "BR0\t"
            else:
                ret += "Other\t"

            if str(interface["ip"]) == "":
                ret += "\t\t"
            else:
                ret += str(interface["ip"]) + "\t"
            status = str(interface["action"])
            if status == "enable":
                ret += "已连接\t"
            else:
                ret += "未连接\t"

            mode = str(interface["role"])
            if mode == "monitoring":
                ret += "监控\t"
            elif mode == "bridge":
                ret += "桥接\t"
            else:
                ret += "网络\t"

            if not "unknown" in interface["speed"].lower():
                ret += str(interface["speed"]) + "\t"
                ret += str(interface["duplex"]) + "\n"
            else:
                ret += "\t\n"
        return ret
Пример #3
0
def _get_device_info(type):
    client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT)
    resp = json.loads(client.get_information(["device_base_info"]))
    if resp["responseCode"] != 200:
        return ""
    else:
        device_info = resp["device_base_info"]
        if type == "model":
            return device_models[device_info["deviceModel"] - 1]
        elif type == "hostname":
            return device_info["hostname"]
        elif type == "device-id":
            return device_info["serials"]
        else:
            return ""
Пример #4
0
def _set_route_info(route_type, action, **kwargs):
    client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT)
    resp = json.loads(client.get_information(["route_information"]))
    if resp["responseCode"] != 200:
        routes_str = ""
    else:
        routes_str = json.dumps(resp["route_information"]["route"])
    if routes_str == "":
        return ""
    routes = json.loads(routes_str)
    route_info = {}
    route_info["type"] = 1 if route_type == "static" else 2
    route_info["destination"] = kwargs["destination"]
    route_info["netmask"] = kwargs["netmask"]
    route_info["gateway"] = kwargs["gateway"]
    route_info["device"] = kwargs["device"]

    if action == "add":
        routes.append(route_info)
    elif action == "del":
        before = len(routes)
        for r in routes:
            if r["type"] == route_info["type"] \
                    and r["destination"] == route_info["destination"] \
                    and r["netmask"] == route_info["netmask"] \
                    and r["gateway"] == route_info["gateway"] \
                    and r["device"] == route_info["device"]:
                routes.remove(r)
                break
        after = len(routes)
        if before == after:
            return u"您要删除的路由并不存在"
    else:
        return ""
    # update routes of device
    client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT)
    config = {"route_information": {"route": routes}}
    res = client.set_information(config)
    res_json = json.loads(res)
    if res_json["responseCode"] != 200:
        return ""
    else:
        return u"操作成功"
Пример #5
0
def _get_services_info():
    client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT)
    resp = json.loads(client.get_information(["service_status"]))
    if resp["responseCode"] != 200:
        return ""
    else:
        ret = ""
        status_info = resp["service_status"]
        for key in status_info.keys():
            display = "-----" + key + "-----\n"
            mods = status_info.get(key)
            for mod in mods:
                display += str(mod["name"])
                display += ":\t"
                status = "running" if mod["status"] == 0 else "stopped"
                display += status
                display += "\n"
            ret += display
        return ret
Пример #6
0
def _get_route_info():
    client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT)
    resp = json.loads(client.get_information(["route_information"]))
    if resp["responseCode"] != 200:
        return ""
    else:
        ret = "目标网络\t子网掩码\t网关\t\t网卡\t类型\n"
        for route in resp["route_information"]["route"]:
            ret += str(route["destination"]) + "\t"
            ret += str(route["netmask"]) + "\t"
            ret += str(route["gateway"]) + "\t"
            if str(route["device"]) == "eth1":
                ret += "MTA\t"
            elif str(route["device"]) == "br0":
                ret += "Br0\t"
            else:
                ret += "Mgmt\t"
            if route["type"] == 1:
                ret += "静态路由"
            else:
                ret += "策略路由"
            ret += "\n"
        return ret
Пример #7
0
def _get_version_info():
    ret = ""
    client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT)
    resp = json.loads(client.get_information(["device_base_info"]))
    if resp["responseCode"] != 200:
        return ""
    else:
        device_info = resp["device_base_info"]
        ret += "天空卫士 " + device_models[device_info["deviceModel"] -
                                       1] + " Release " + str(
                                           device_info["version"]) + " 版本\n"
        ret += "CPU:\t\t" + str(device_info["cpucore"]) + "核\n"
        ret += "Memory:\t\t" + str(device_info["memory"]) + "\n"
        ret += "启动时间:\t" + str(device_info["starttime"]) + "\n"
        ret += "当前时间:\t" + _get_current_time() + "\n"
        ret += "型号:\t\t" + device_models[device_info["deviceModel"] - 1] + "\n"
        try:
            license_id, products = _get_license_info()
        except:
            return ret
        ret += "License Key:\t" + str(license_id) + "\n"
        ret += "授权功能:\t" + ", ".join(products) + "\n"
        return ret