ruby 練習つづき
(s1とだいたい同じもの,2007/8/16)
非推奨の書法も含まれます
def ask comm puts comm; gets.chomp end while ipAdd =ask('調べたいIP(0.0.0.0)') puts `nslookup #{ipAdd}` end
def ask comm;puts comm;gets.chomp;end;while ipAdd =ask('調べたいIP(0.0.0.0)?');puts `nslookup #{ipAdd}`;end
def ask word, com print word puts `#{com} #{gets.chomp}` end loop {ask('調べたいIP(0.0.0.0):','nslookup')}
def ask(word,com) print word ans =gets.chomp # 空白か","で区切る ans.split(/\s|,/).map{|an| puts "(#{an})" puts `#{com} #{an}` } end puts '空白で区切って複数一度に調べます(0.0.0.0 0.0.0.0)' loop { ask('調べたいIP:','nslookup') }
def ask( word, com ) print \ word puts \ `#{com} \ #{gets.chomp}` end loop { ask( "調べたい\ \ \ \ IP(0.0.0.0):", 'nslookup' ) }
# ruby -Ks name.rb # などと漢字オプション指示しないと実行できない # 別名の設定 alias looo loop alias k puts def どう(word,com) print word ans =gets.chomp # 空白か","で区切り、それぞれについて ans.split(/\s|,/).map{|an| k `#{com} #{an}` } end k '空白で区切って複数一度に調べます(0.0.0.0 0.0.0.0)' looo { どう('調べたいIP:','nslookup') }
# おもに某カウンターログ用 fn ="nsl-log.txt" # ログファイル名 $log =open(fn,"a") # append mode, WRONLY|CREAT|APPEND # $...グローバル変数 def ask(word) print word ans =gets ans.split(/\s|,/).map{|an| mip = (an =~/\d+\.\d+\.\d+\.\d+/) # 0.0.0.0という文字列か mtime = (an =~/:/) # :を含むか(時間,12:12:22) mday = (an =~/-/) # -を含むか(日時,2007-08-18) if (mip != nil) puts ">#{an}: ok" logw = `nslookup #{an}` puts logw co=0 logw.split(/\n|,/).map{|ww| co +=1 if co>3 $log.puts(ww) end } elsif (mday != nil) $log.puts $log.print(an) $log.print(" ") elsif (mtime != nil) $log.puts(an) else puts ">#{an}: not address" end } end puts '空白で区切って複数一度に調べられます(0.0.0.0 0.0.0.0)' loop { ask('調べたいIP:') } $log.close puts "..." # Ctrl+Cでloop中止。表示されない。closeしない?