--[[   ■ vRX 基本ライセンスの有効期間を自動でメール通知 サンプルスクリプト Ver. 1.00                            2023.04.27 ヤマハ株式会社   本スクリプトは、vRX の基本ライセンスの有効期間が切れる前にメールで通知する   Luaスクリプトのサンプルです。   show status license vrx all コマンドで表示される有効期間と現在時刻の差が指定   の日数以内になったときにメールで通知を行います。   また、毎月1日にもメール通知を行います。   [注意事項]   ・本スクリプトは、schedule atコマンドでルーターの起動時に実行されるようにし    てください。   ・スクリプト中の「設定値」の値は、ご使用の環境に応じて変更した上でご使用く ださい。 ・スクリプトの起動直後にメール送信を行います。メールを受信できなかった場合は 設定が正しいか確認してください。   ・ご利用の環境に合わせて、★マークの付いた設定値を変更してください。    [更新履歴]   2023.04.27 Ver.1.00 初版作成 ]] ---------------------------------------- -- 設定値 -- ---------------------------------------- -- 送信メールの設定 local mail_table = { smtp_address = "(SMTPサーバーのアドレス)", -- ★ smtps = true, smtp_port = "587",--(0 .. 65535) -- ★ smtp_auth_protocol = "plain", -- ★ smtp_auth_name = "(SMTP認証用ユーザー名)", -- ★ smtp_auth_password = "(SMTP認証用パスワード)", -- ★ from = "(送信元メールアドレス)", -- ★ to = "(宛先メールアドレス)", --複数登録する場合は、,で区切る。空白禁止-- ★ subject = "vRX ライセンス有効期間のお知らせ"-- メールのタイトル-- ★ } -- 有効期限の何日前からメールを送信するかの設定 (日) local before_days = (1 - 365) -- ★ -- 基本ライセンスの有効期限の確認およびメール通知の間隔設定 (日) local check_interval_days = (1 - 10) -- ★ ---------------------------------------- -- 変数定義 -- ---------------------------------------- -- 有効期限を確認するコマンド local command = "show status license vrx all" -- 有効期限文字列 local pattern = "(%d%d%d%d)/(%d%d)/(%d%d)%s+%-%s+(%d%d%d%d)/(%d%d)/(%d%d)" local year1, month1, day1, year2, month2, day2 ---------------------------------------- -- メインルーチン -- ---------------------------------------- if check_interval_days < 1 or check_interval_days > 10 then print("check_interval_days は 1 から 10 の範囲内で指定してください。") os.exit(1) end if before_days < 1 or before_days > 365 then print("before_days は 1 から 365 の範囲内で指定してください。") os.exit(1) end if before_days < check_interval_days then print("check_interval_days は before_days より小さい数値で指定してください。") os.exit(1) end -- 初回フラグ local first_time = true -- 起動直後 30 秒待機 rt.sleep(30) while true do -- 現在時刻の取得 local current_date = os.date("*t") local current_time = os.time(current_date) local end_time -- 0時0分0秒の日付情報を設定 current_date.hour = 0 current_date.min = 0 current_date.sec = 0 current_date = os.time(current_date) -- 有効期限を取得するためコマンド実行 rtn, str = rt.command(command) -- 有効期限を抽出する for y1, m1, d1, y2, m2, d2 in str:gmatch(pattern) do year1, month1, day1, year2, month2, day2 = y1, m1, d1, y2, m2, d2 end if year1 and month1 and day1 and year2 and month2 and day2 then local expiration_date = os.time({year = tonumber(year2), month = tonumber(month2), day = tonumber(day2), hour = 0, min = 0, sec = 0}) local notice_days = 60 * 60 * 24 * before_days if expiration_date - current_date <= notice_days and expiration_date - current_date >= 0 then -- 有効期限が before_days 日前の場合 local days_left = (expiration_date - current_date) / (60 * 60 * 24) -- メール本文を作成 mail_table.text = string.format("残り %d 日で vRX ライセンスの有効期間が切れます。ライセンス更新手続きをしてください。\r\n\r\n%s", days_left, str) -- メール送信 rt.mail(mail_table) elseif expiration_date - current_date < 0 then -- 有効期限が切れた場合 -- メール本文を作成 mail_table.text = string.format("vRX ライセンスの有効期限が切れました。ライセンス更新手続きをしてください。\r\n\r\n%s", str) -- メール送信 rt.mail(mail_table) elseif os.date("%d", current_date) == "01" then -- 毎月1日のメール通知 local days_left = (expiration_date - current_date) / (60 * 60 * 24) -- メール本文を作成 mail_table.text = string.format("残り %d 日で vRX ライセンスの有効期間が切れます。\r\n\r\n%s", days_left, str) -- メール送信 rt.mail(mail_table) elseif first_time then -- メール設定の確認のため初回にメール通知 local days_left = (expiration_date - current_date) / (60 * 60 * 24) -- メール本文を作成 mail_table.text = string.format("残り %d 日で vRX ライセンスの有効期間が切れます。\r\n\r\n%s", days_left, str) -- メール送信 rt.mail(mail_table) end end first_time = false -- 時刻補正を行うための時刻取得 end_time = os.time(os.date("*t")) -- check_interval_days 日待つ rt.sleep(60 * 60 * 24 * check_interval_days - (end_time - current_time)) end