{
    "name"  : "检测用户接入端口是否正确",
    "problems" : {
        "dhcp12" : {
            "desc"      : "【问题】MAC表查询端口未学习到用户MAC地址",
            "advice"    : ["【建议】请判断下端口未学习到MAC的原因，建议检查："
                          ,"\t1、端口线路是否正常，尝试更换网线。"
                          ,"\t2、端口配置vlan信息与用户接入的vlan是否一致。"]
        },
        "dhcp34" : {
            "desc"      : "【问题】户所在的vlan与网关vlan不一致。",
            "advice"    : ["【建议】请修改无法获取IP地址终端口vlan。接入设备在接入终端端口下配置switchport access vlan 网关对应vlan："
                          ,"接入设备在接入终端端口下配置switchport access vlan 网关对应vlan："
                          ,"核心、汇聚设备上配置switchport mode trunk"]
        }
    }
}
---------------------------code-------------------------------
#检测用户接入端口是否正确
param = {}
param["role"] = precondition('role')
param["serverIp"] = precondition('serverIp')
param["client_mac"] = precondition('client_mac')
param["gateway"] = precondition('gateway')
no_support_dev = false
deviceMode = get_device_model()
if deviceMode == 'RG-S2900G-E/P' || deviceMode == 'RG-S2600G-I' :
    no_support_dev = true
end
vlan_id = null
interface = null
if no_support_dev:
    return "success"
end

def get_simppe_port(port):
    sets = {}
    sets["AggregatePort "] = "Ag"
    sets["GigabitEthernet "] = "Gi"
    sets["TenGigabitEthernet "] = "Te"
    foreach key in sets :
        if port.search(key) != -1:
            setsTmp = sets[key]
            res = port.replace(key,setsTmp)
            return res
            break   # @fanbh return之后的break没必要了
        end
    end
end

role = param["role"]
client_mac = param["client_mac"]
gateway = param["gateway"]
if role == 'relay' || role == 'server':
    cmd = "show mac-address-table address " + client_mac
    response = cli(cmd)
    results = response.split(/\r?\n/ig)
    exist = false  
    str = "/" + client_mac + "/gi"
    re = eval_script(str)  
    i = 1
    while i < results.length:    # @fanbh 建议results.length-1即可，另外，这里完全可以通过正则匹配到相关项并把端口号获取，更简单
        resultsTmp = results[i]
        if resultsTmp.search(re) != -1 :
            arr = resultsTmp.trim()
            arr = arr.split(/\s\s+/ig)
            vlan_id = arr[0]
            interface = arr[3]
            exist = true
        end
        i = i + 1
    end
    if exist == false:
        add_problem("dhcp12")
        return "warn"
    end
    response = cli("show ip interface brief | in VLAN")    # @fanbh 与文档不符
    results = response.split(/\r?\n/ig)
    exist = false
    i = 0
    while i < results.length:
        resultsTmp = results[i]
        if resultsTmp.search(gateway) != -1 :
            arr = resultsTmp.split(" ")
            vlan_id_gateway = arr[1]
            exist = true
            break
        end
        i = i + 1
    end
    if exist == false:
        return "success"
    end
    cmd = "show vlan id " + vlan_id_gateway
    response = cli(cmd)
    results = response.split(/\r?\n/ig)
    exist = false
    res = get_simppe_port(interface)
    _re = res.replace(/\//g,"\\/")
    port = "Gi0\\/1"
    str = "/" + _re + "[^0-9]/gim"
    re = eval_script(str)  
    i = 1
    while i < results.length:
        if resultsTmp.search(re) != -1:
            exist = true
            break
        end
        i = i + 1
    end
    if exist == false:
        add_problem("dhcp34")
        return "warn"
    end
    return "success"
else :
    return "success"
end

return "success"


