运行此代码时出现Ruby rake样式错误
这是我的密码:运行此代码时出现Ruby rake样式错误,ruby,smtp,rubygems,rakefile,Ruby,Smtp,Rubygems,Rakefile,这是我的密码: #!/usr/bin/env ruby require 'net/ping' require 'net/smtp' # check if host is up def up?(host) check = Net::Ping::External.new(host) check.ping? end # list of switches to check switch_list = [ ['A ', '192.168.1.1'], ['B ', '192.168.
#!/usr/bin/env ruby
require 'net/ping'
require 'net/smtp'
# check if host is up
def up?(host)
check = Net::Ping::External.new(host)
check.ping?
end
# list of switches to check
switch_list = [
['A ', '192.168.1.1'],
['B ', '192.168.1.2'],
['C ', '192.168.1.3']
]
failed_switches =[]
switch_list.each do |switch, ip|
puts "#{switch}" + up?(ip).to_s
failed_switches << [switch, ip] unless up?(ip)
end
failed_switches.each do |ip| unless failed_switches.empty?
puts "#{failed_switches}" + up?(ip).to_s + "\n"
end
#email the list of checked and failed swithces
message = <<MESSAGE_END
From: abc@gmail.com
To: xyz@gmail.com
Subject: device down!
checked devices:
#{switch_list.map { |switches, ips| 'Device:' + switch + 'IP:' + ip }.join("\n") }
failed devices:
#{failed_switches.map { |switches, ips| 'Device:' + switch + 'IP:' + ip }.join("\n") }
MESSAGE_END
puts "#{message}"
next unless failed_switches.empty?
Net::SMTP.start('your mail server') do |smtp|
smtp.send_message message, 'xyz@gmail.com'
end
end
#/usr/bin/env ruby
需要“净/平”
需要“net/smtp”
#检查主机是否已启动
def up?(主机)
check=Net::Ping::External.new(主机)
查平先生?
终止
#要检查的开关列表
开关列表=[
[A',192.168.1.1'],
[B','192.168.1.2'],
[C','192.168.1.3']
]
失败的_开关=[]
交换机列表。每个do交换机,ip|
将“#{switch}”+向上(ip)。向上
失败的_开关失败的_开关。每个都有| ip |,除非失败的_开关。空?
?不要那样做。先测试,然后循环,不要使用尾随条件。@theTinMan你能给我一个例子吗。。至于我该怎么做..使用if
或,除非
阻塞并在其内部执行循环。类似这样的情况<代码>除非失败。\u开关为空?失败的|交换机。每个do | ip |将“#{失败的|交换机}”+向上(ip)。到| s+“\n”结束
Yes。尾随条件仅在简单逻辑测试中可接受。用一个来控制你是否进入一个循环并不是那种情况。
Inconsistent indentation detected:
message = <<MESSAGE_END
Unused block argument - line 36 line 39
**|switches**, **ips|**
Inconsistent indentation detected: line 44
next unless failed_switches.empty?
Inconsistent indentation detected: line 45
Net::SMTP.start('your mail server') do |smtp|