メッセージ監視2

パソコン整理していたら出てきた^^;

(1)read_msg.rb

pattern = Regexp.new("apache")
filename = "/var/log/messages"
file = open(filename)

while text = file.gets do
if pattern =~ text
print text
end
end
file.close

(2)send_mail.rb
require 'net/smtp'

message = <
To:root@example.com
Subject:TEST MAIL

This is TEST MAIL by RUBY
MESSAGE_END

Net::SMTP.start("localhost") do |smtp|
smtp.send_message message, 'bozu@example.com','root@example.com'
end