接続ホスト履歴出力

図 スイッチ3台冗長構成

LANネットワークのホストの利用状況を履歴ファイルに出力する設定例です。
本設定例のLuaスクリプトではSWX2200に接続されたホストに対するDHCPのSYSLOGを監視し、検出した際に該当のログとホストが接続されているSWX2200のポート番号を指定のファイルに出力します。

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

設定値

-- 履歴を保存するファイル名(絶対パスで指定)
filename = "/host_record.txt"
-- 検出したい 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