Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class UsersController < ApplicationController
- # before_action :require_user, except: [:index]
- # before_action :user_signed_in?, except: [:index]
- # before_action :verify_authenticity_token
- require 'open3'
- def index
- @ip = request.remote_ip
- if user_signed_in?
- cmd = "/etc/omapi/dhcp-show-lease.sh #{@ip}"
- result = run_cmd(cmd)
- @ends = result[:ends]
- @starts = result[:starts]
- render 'show_lease'
- else
- render 'index'
- end
- end
- def normal_dns
- ip = request.remote_ip
- cmd = "/etc/omapi/dhcp-normal-dns.sh #{ip}"
- run_cmd(cmd)
- redirect_to root_url
- end
- private
- def run_cmd(cmd)
- result = { ip: nil, mac: nil, starts: nil, ends: nil }
- output = ''
- Open3.popen2e(cmd) do |_stdin, stdout_err, wait_thr|
- while line = stdout_err.gets
- output += line
- tmp = line.split(': ')
- result[tmp[0].to_sym] = tmp[1].delete("\n") if result.key? tmp[0].to_sym
- end
- exit_status = wait_thr.value
- if exit_status.success?
- flash[:success] = "Η εντολή '#{ cmd }'εκτελέστηκε κανονικά το trace είναι: \n #{output}"
- else
- flash[:error] = "Υπήρξε πρόβλημα με την εκτέλεση της εντολής '#{ cmd }', ο κωδικός σφάλματος είναι #{exit_status} και το trace είναι: \n #{output}"
- end
- logger.debug "output: #{output}"
- return result
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement