{
    "name"  : "与服务器是否能正常通信",
    "problems" : {
        "dhcp12" : {
            "desc"      : "【问题】设备未配置DHCP服务器",
            "advice"    : ["【建议】建议配置DHCP服务器 "
                          ,"配置示例："
                          ,"\tRuijie#config"
                          ,"\tRuijie(config)#ip helper-address ${0}"
                          ,"\tRuijie(dhcp-config)#end"
                          ,"\tRuijie#write"]
        },
        "dhcp34" : {
            "desc"      : "【问题】设备未配置DHCP服务器",
            "advice"    : ["【建议】建议配置下列DHCP服务器：${0} "
                          ,"配置示例："
                          ,"\tRuijie#config"
                          ,"\tRuijie(config)#ip helper-address 服务器地址"
                          ,"\tRuijie(dhcp-config)#end"
                          ,"\tRuijie#write"]
        },
        "dhcp56" : {
            "desc"      : "【问题】设备无法与服务器 ${0} 正常通信",
            "advice"    : ["【建议】可能原因是未给上联口配置IP地址或者上联口故障，"
                           ,"建议给上联口配置与认证服务器IP地址相同网段的地址或者更换服务器的上联端口"]
        }
    }
}
---------------------------code-------------------------------
#与服务器是否能正常通信
param = {}
param["role"] = precondition('role')
param["serverIp"] = precondition('serverIp')
no_support_dev = false
deviceMode = get_device_model()
if deviceMode == 'RG-S2900G-E/P' || deviceMode == 'RG-S2600G-I' :
    no_support_dev = true
end
dhcp_server_addrs = []
not_connect_addrs = []
times = 0
if no_support_dev:
    return "success"
end

def ping_server():
    length = dhcp_server_addrs.length
    if times < length:
        addressIP = dhcp_server_addrs[times]
        cmd = 'ping ' + addressIP
        response = cli(cmd)
        if response.search("Invalid input") != -1:
            return "skip"
        end
        exist = false
        results = response.split(/\r?\n/ig)
        j = 1 
        while j < results.length:    # @fanbh 这边有必要去拆成数组再一个一个的找呢？直接输出内容进行search不是更简洁？
            resultsTmp = results[j]
            if resultsTmp.search('!') != -1:
                exist = true
                break
            end
            j = j + 1 
        end
        if exist == false:
            ipAddress = dhcp_server_addrs[times]
            not_connect_addrs.push(ipAddress)
        end
        times = times + 1
        res = ping_server()
        return res
    else:
        if not_connect_addrs.length > 0:
            str = not_connect_addrs.join(',')
            add_problem("dhcp56",[str])
            return "warn"
        end
    end
end

role = param["role"]
if role == 'relay' :
    response = cli("show run | include ip helper-address")
    results = response.split(/\r?\n/ig)
    exist = false    
    i = 1
    while i < results.length:  # @fanbh 循环到results.length-1即可了
        resultsTmp = results[i]
        if resultsTmp.search("ip helper-address") != -1 :
            exist = true       # @fanbh 文档中是"未发现配置了同网段的IP地址的端口",而不是仅仅匹配到ip helper-address
            resultsTmp = resultsTmp.trim()
            dhcp_server_addr_arr = resultsTmp.split(' ')
            index = dhcp_server_addr_arr.length -1
            dhcp_server_addr = dhcp_server_addr_arr[index]
            dhcp_server_addrs.push(dhcp_server_addr)
        end
        i = i + 1
    end
    serverIp = param["serverIp"]
    if exist == false:
        if serverIp.length > 0 :
            str = serverIp[0]
            add_problem("dhcp12",[str])
        else :
            str = "服务器地址"
            add_problem("dhcp12",[str])            
        end
        return "warn"
    end
    if serverIp.length > 0 :
        not_match_servers = []
        j = 0 
        while j < serverIp.length:
            _exist = false
            serverIpTmp = serverIp[j]
            k = 0
            while k < dhcp_server_addr.length:                
                dhcp_server_addrTmp = dhcp_server_addr[k]
                if serverIpTmp == dhcp_server_addrTmp:
                    _exist = true
                    break
                end
                k = k + 1 
            end
            if _exist == false:
                not_match_servers.push(serverIpTmp)
            end
            j = j + 1
        end
        if not_match_servers.length > 0:
           str = not_match_servers.join(',')
           add_problem("dhcp34",[str])
        else :
            res = ping_server()
            return res
        end
    else:
        res = ping_server()
        return res        
    end
end

return "success"
















