Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.51 KB | None | 0 0
  1. class UsersController < ApplicationController
  2.   # before_action :require_user, except: [:index]
  3.   # before_action :user_signed_in?, except: [:index]
  4.   # before_action :verify_authenticity_token
  5.   require 'open3'
  6.  
  7.    def index
  8.      @ip = request.remote_ip
  9.      if user_signed_in?
  10.        cmd = "/etc/omapi/dhcp-show-lease.sh #{@ip}"
  11.        result = run_cmd(cmd)
  12.        @ends = result[:ends]
  13.        @starts = result[:starts]
  14.        render 'show_lease'
  15.      else
  16.        render 'index'
  17.      end
  18.    end
  19.  
  20.    def normal_dns
  21.      ip = request.remote_ip
  22.      cmd = "/etc/omapi/dhcp-normal-dns.sh #{ip}"
  23.      run_cmd(cmd)
  24.      redirect_to root_url
  25.    end
  26.  
  27.   private
  28.  
  29.   def run_cmd(cmd)
  30.     result = { ip: nil, mac: nil, starts: nil, ends: nil }
  31.     output = ''
  32.     Open3.popen2e(cmd) do |_stdin, stdout_err, wait_thr|
  33.       while line = stdout_err.gets
  34.         output += line
  35.         tmp = line.split(': ')
  36.         result[tmp[0].to_sym] = tmp[1].delete("\n") if result.key? tmp[0].to_sym
  37.       end
  38.       exit_status = wait_thr.value
  39.       if exit_status.success?
  40.         flash[:success] = "Η εντολή '#{ cmd }'εκτελέστηκε κανονικά το trace είναι: \n #{output}"
  41.       else
  42.         flash[:error] = "Υπήρξε πρόβλημα με την εκτέλεση της εντολής '#{ cmd }', ο κωδικός σφάλματος είναι #{exit_status} και το trace είναι: \n #{output}"
  43.       end
  44.       logger.debug "output: #{output}"
  45.       return result
  46.     end
  47.   end
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement