连接主机的履历输出

将LAN网络的主机利用情况向履历文件传输的设定例。
本设定例的Lua脚本中,对SWX2200所连接主机的DHCP的SYSLOG进行监视、检测到时,将该运行记录和主机所连接的SWX2200的端口编号输出到指定的文件中。

RTX1200的设定例

LAN接口的设定
(使用LAN1端口)

ip lan1 address 192.168.100.1/24

DHCP的设定

dhcp service server
dhcp server rfc2131 compliant except remain-silent
dhcp scope 1 192.168.100.2-192.168.100.191/24

SWX2200的设定

switch control use lan1 on

Lua脚本日程的设定

schedule at 1 startup * lua /swx2200_lua_host_record_rtx1200.lua

Lua脚本的设定例

设定值

-- 保存履历的文件名(用绝对路径指定,此处设置为保存在sd卡中)
filename = "sd1:/host_record.txt"
-- 输出的SYSLOG的等级(info, debug, notice)
log_level = "info"
-- 希望查出的SYSLOG的字符串模式
ptn = "%[DHCPD%]"
-- 检测MAC地址的字符串格式
mac_ptn = "%x%x:%x%x:%x%x:%x%x:%x%x:%x%x"

主机搜索

function search_host(mac)
  sw_route = nil
  route = nil
  port = nil

  rtn, str = rt.command("show status switching-hub macaddress " .. mac)
  port = string.match(str, "port (%d):")
  if (port) then
    route = "LAN1:" .. port
  else
    rtn, str = rt.command("show arp lan2")
    if (string.match(str, mac)) then
      route = "LAN2"
    else
      rtn, str = rt.command("show arp lan3")
      if (string.match(str, mac)) then
        route = "LAN3"
      end
    end
  end

  if (not route) then
    return
  end

  while true do
    rtn, str = rt.command("switch control function get status-macaddress-addr " ..
                   mac .." " .. route)
    if (rtn) and (str ~= "0 entry\r\n") then
      port = string.match(str, "(%d+)")
      sw_route = route
      route = route .."-" ..port
    else
      break
    end
  end
  return sw_route, port
end

主程序

local rtn, str
local fhs, estr, ecode
local buf

while (true) do
  rtn, str = rt.syslogwatch(ptn)
  mac = string.match(str[1], mac_ptn)
  if (mac) then
    sw_route, port = search_host(mac)
    if (sw_route) and (port) then
      fh, estr, ecode = io.open(filename, "a")
      if (not fh) then
        rt.syslog(log_level, "file open error (" .. estr ..")")
      else
        buf = str[1]
        rtn, str = rt.command("switch control function get system-name " ..
                       sw_route)
        name = string.match(str, "(.-)\r\n")
        if (name) then
          buf = buf .. " at " .. name
        else
          buf = buf .. " at " .. sw_route
        end
        buf = buf .. " : port " .. port
        buf = buf .. "\r\n"
        fh:write(buf)
        fh:flush()
        fh:close()
      end
    end
  end
end

返回顶部Return to Top

网络相关产品

服务支持

事业绍介