Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Crystal in Operation

Crystal in Operation

サーバ管理業務に便利なCrystal言語の機能紹介。

ʕ·ᴥ·ʔAKJ

July 18, 2018
Tweet

Other Decks in Programming

Transcript

  1. 61:+ • ʕ·ᴥ·ʔAKJ )C @arcage arcage • =A37.8(/>D &$" •

    .80!'2,95*=A ? • B/0 • ;/ &$"   • &4  • 6@- %4<#' 2018/07/18 Crystal.tokyo #7
  2. :6* !. maillog )$# SMTP AUTH;/( IP " %8 9&0.

     - +," ' /)51  23 10742" 1$43" 24$44" 2018/07/18 Crystal.tokyo #7
  3. IO#set_encoding  # • #set_encoding • encoding: ICONV.-" • invalid:

    :skip or nil &)$UTF-8, • ' /+ File.open("./data.txt") do |file| # Shift_JIS*0 %  # (!0 file.set_ecoding("Shift_JIS", :skip) end # File.open  # File.open("./data.txt", "w" encoding: "Shift_JIS", invalid: :skip) 2018/07/18 Crystal.tokyo #7
  4. File::Info#modification_time  & 0.24File::Stat •    % •

    #ino #? • #mtime → #modification_time • #atime#utime #? f_info = File.info("./README.md") f_info.modification_time #=> 2018-02-02 05:11:53.379574681 UTC f_info.permissions #=> rw-r--r-- (0o644) f_info.size #=> 11325_u64 # !"( inode$) f_info.same_file?(other_f_info) 2018/07/18 Crystal.tokyo #7
  5. class Zlib::Reader gz •  •   Zlib::Reader IO

    •   require "zlib" File.open("./maillog.gz") do |file| Zlib::Reader.open(file) do |gz| gz.each_line do |line| # ... end end end 2018/07/18 Crystal.tokyo #7
  6. class Zip::Reader zip  •   • ! 

    Zip::Reader::Entry • Zip  /  •   #io  require "zip" Zip::Reader.open("./logs.zip") do |zip| zip.each_entry do |entry| entry.io.each_line do |line| # ... end end end 2018/07/18 Crystal.tokyo #7
  7. method system    •  0 true •

     0 false system("which", ["cc"]) #=> true #   system("whic", ["cc"]) #=> false #  (Process::Status) $?.exit_code #=> 32512 2018/07/18 Crystal.tokyo #7
  8. method ` ←      • 

      cc_path = `which cc` #=> "/usr/bin/cc\n" #    cc_path = `whic cc` #=> "" #  $?.exit_code #=> 32512 2018/07/18 Crystal.tokyo #7
  9. class Process   •   • #wait •

    STDIN/OUT/ERR  prc = Process.new("which", ["cc"]) exit_status = prc.wait exit_status.exit_code #=> 0 String.build { |io| Process.new("which", ["cc"] output: io).wait } #=> "/usr/bin/cc\n" 2018/07/18 Crystal.tokyo #7
  10. class Process   • ID •   

    • CPU …  •    •  kill Process.pid #=> 9651 Process.executable_path #=> "/home/user/bin/exec_file" Process.times #=> Process::Tms(@utime=0.0, @stime=0.0, @cutime=0.0, @cstime=0.0) Process.find_executable("cc") #=> "/usr/bin/cc" Process.kill(6523) 2018/07/18 Crystal.tokyo #7
  11. enum Signal  $!  ! • #trap do ...

    end • #reset Signal::INT.trap do # Ctrl+C    " end Signal::HUP.trap do # !#   end #   Signal::INT.reset 2018/07/18 Crystal.tokyo #7
  12. 97-I3F  • 8.<I>BG#$@ • )*6(;&:5 Ruby 90D → Crystal

    25D • =2H% '!%8.4   • crystal run trial & error +C • 0EA 1C? , • "& %-I / 2018/07/18 Crystal.tokyo #7
  13.     crystal-logreader •  ! • 

      tail -F   require "logreader" log = LogReader.new("/var/log/maillog") log.each do |line| #... end 2018/07/18 Crystal.tokyo #7
  14.   ip2country •  • en/fr/es/de/ja/zh/ko ! / RIR

    delegations umpirsky/country-list require "ip2country" ip2c = IP2Country.new ip2c.lookup("8.8.8.8") #=> "United Status" ip2c.lookup("8.8.8.8", "ja") #=> "  " ip2c = IP2Country.new("ja") ip2c.lookup("8.8.8.8") #=> "  " 2018/07/18 Crystal.tokyo #7
  15. %(   crystal-email • UTF-8# • $& • HTML

    • AUTH PLAIN/LOGIN # • STARTTLS # • 'concurrent"  Gmail   • Thanks @sdogruyol! require "email" EMail.send("smtp.example.org") do from "[email protected]" to "[email protected]" subject ") !" message "" " attach "photo.jpg" end 2018/07/18 Crystal.tokyo #7
  16.   crystal-patlite • PATLITE • PHN   

     //  // require "patlite" ip = "192.169.100.100" port = 10000 plite = Patlite::PHN.new(ip, port) plite.set do green_on end 2018/07/18 Crystal.tokyo #7
  17.  8  # 29 - +," (! /*51 10642"

    1%43" 24%44" maillog *%$ SMTP AUTH :/) IP " &7 9' 0. 23 2018/07/18 Crystal.tokyo #7