Guest User

Untitled

a guest
Oct 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'rubygems'
  3. require 'optparse'
  4. require 'socket'
  5. require 'json'
  6. require 'readline'
  7.  
  8. @interactive = STDIN.isatty and STDOUT.isatty
  9.  
  10. def readline_hist
  11. if @interactive
  12. line = Readline.readline('$ ', true)
  13. else
  14. line = Readline.readline
  15. end
  16. return nil if line.nil?
  17. if line =~ /^\s*$/ or Readline::HISTORY.to_a[-2] == line
  18. Readline::HISTORY.pop
  19. end
  20. line
  21. end
  22.  
  23. $runopts = {}
  24. $runopts[:verbose] = 0
  25. $runopts[:port] = 3300
  26.  
  27. OptionParser.new do |opts|
  28. opts.banner = "#{__FILE__} -p port"
  29.  
  30. opts.on('-p', '--port [LEVEL]', /^[0-9]+$/, 'Port') do |opt|
  31. $runopts[:port] = opt ? opt.to_i : 1
  32. end
  33. opts.on('-f', '--force', 'Reconnect on fail') do |opt|
  34. $runopts[:force] = opt
  35. end
  36. end.parse!
  37.  
  38. sock = nil
  39.  
  40. if $runopts[:force]
  41. while not sock
  42. waited = false
  43. begin
  44. sock = TCPSocket.open('localhost', $runopts[:port])
  45. rescue
  46. waited = true
  47. sleep 1
  48. print '.' if @interactive
  49. STDOUT.flush
  50. end
  51. print "\n" if waited
  52. end
  53. else
  54. begin
  55. sock = TCPSocket.open('localhost', $runopts[:port])
  56. rescue
  57. puts "Unable to connect."
  58. Process.exit
  59. end
  60. end
  61.  
  62. last_cmd = ''
  63. next_cmd = nil
  64.  
  65. while (c = next_cmd) or (c = readline_hist)
  66. if c.count(";") > 0
  67. c, next_cmd = c.split(";", 2)
  68. end
  69. c = c.strip
  70.  
  71. skip = false
  72. cmd = ''
  73.  
  74. case c
  75. when ''
  76. skip = true
  77. when 'ping'
  78. cmd = 'system.ping'
  79. when 'help'
  80. puts "WAT"
  81. skip = true
  82. when 'l'
  83. cmd = last_cmd
  84. when 'qs'
  85. sock.puts({"exec" => "system.exit"}.to_json)
  86. puts "Bye." if @interactive
  87. Process.exit
  88. when 'q'
  89. puts "Bye." if @interactive
  90. Process.exit
  91. else
  92. cmd = c
  93. end
  94.  
  95. if not skip
  96. cmd_class, cmd_name = cmd.split('.')
  97. last_cmd = cmd
  98. begin
  99. sock.puts({"exec" => "#{cmd_class}.#{cmd_name}"}.to_json)
  100. rescue Exception => e
  101. if $runopts[:force]
  102. print 'Connection lost, reconnecting ' if @interactive
  103. STDOUT.flush
  104. sock.close
  105. sock = nil
  106. while not sock
  107. begin
  108. sock = TCPSocket.open('localhost', $runopts[:port])
  109. rescue
  110. end
  111. sleep 1
  112. print '.' if @interactive
  113. STDOUT.flush
  114. end
  115. print "\n"
  116. sock.puts({"exec" => "#{cmd_class}.#{cmd_name}"}.to_json)
  117. else
  118. Process.exit
  119. end
  120. end
  121. sleep 0.2
  122. begin
  123. ret = JSON.parse(sock.gets)
  124. rescue Exception => e
  125. end
  126. if @``interactive
  127. puts "Status: #{ret['status']}"
  128. puts "Return: #{ret['return'].inspect}"
  129. else
  130. puts ret['status']
  131. puts ret['return'].inspect
  132. end
  133. end
  134. end
Add Comment
Please, Sign In to add comment