require 'net/ssh' require 'ipaddr' require 'json' require 'ipaddr' require 'date' require "freewifi/additional/additional_functions" class NEWhubWifi_1 attr_accessor :t_id_min, :t_id_max, :additional_func_wifi_1 def initialize @t_id_min = 10001 @t_id_max = 65535 @additional_func_wifi_1 = InternalFunc_1.new end def del_eoip_for_mikr_1(tunnel_id, hub1, hub2, user_hub, password_hub) begin Net::SSH.start(hub1, user_hub, :password => password_hub) do |ssh| #result = ssh.exec!("/interface eoip print where tunnel-id=#{tunnel_id}") #p result result = ssh.exec!("/interface eoip print where tunnel-id=#{tunnel_id}") regimsi = /\s+name=\"(.+)\"\s+/ m1 = regimsi.match(result)[1] sleep 0.2 result2 = ssh.exec!("/interface bridge port remove [/interface bridge port find interface=#{m1}]") sleep 0.2 result3 = ssh.exec!("/interface eoip remove [/interface eoip find where tunnel-id=#{tunnel_id}]") #result2 = ssh.exec!("interface bridge port add bridge=#{bridge_name} interface=#{tunnel_name}") end #Net::SSH.stop rescue nil end begin Net::SSH.start(hub2, user_hub, :password => password_hub) do |ssh| result = ssh.exec!("/interface eoip print where tunnel-id=#{tunnel_id}") regimsi = /\s+name=\"(.+)\"\s+/ m1 = regimsi.match(result)[1] sleep 0.2 result2 = ssh.exec!("/interface bridge port remove [/interface bridge port find interface=#{m1}]") sleep 0.2 result3 = ssh.exec!("/interface eoip remove [/interface eoip find where tunnel-id=#{tunnel_id}]") #result = ssh.exec!("interface eoip add remote-address=#{ap_ip_address} tunnel-id=#{tunnel_id} name=#{tunnel_name}") #result2 = ssh.exec!("interface bridge port add bridge=#{bridge_name} interface=#{tunnel_name}") end #Net::SSH.stop rescue nil end end def ap_get_info(ap_ip_address, ap_login, ap_password) res = "" Net::SSH.start(ap_ip_address, ap_login, :password => ap_password) do |ssh| result = ssh.exec!("system identity print") dff = result.to_s res = dff.gsub!("name: ", "").gsub!("\r\n\r\n", "").gsub!(" ", "") end res end def ap_tun_rewrite(ap_ip_address, tun_id, ap_login, ap_password) res = "" Net::SSH.start(ap_ip_address, ap_login, :password => ap_password) do |ssh| result = ssh.exec!("/interface eoip set tunnel_to_hab1 tunnel-id=#{tun_id} remote-address=172.24.202.145") end res end def get_tun_id(req_num_of_tunid, busy_tun_id) all_ids=[] i = t_id_min while i < t_id_max all_ids << i i=i+1 end p all_ids end #procedure for tunnel id-s selection based on all_tunids array def get_tun_id2_1(req_num_of_tunid, busy_tun_id) #req_num_of_tunid - integer number of necessary tun ids #busy_tun_id - [] dictionary of currently used tun ids with integer elements input_params = {:req_num_of_tunid => req_num_of_tunid, :busy_tun_id => busy_tun_id} output_params = {} all_ids = [] req_tun_ids = [] begin available_tun_id = Freewifi_1::ALL_TUNIDS - busy_tun_id i = 0 while i < req_num_of_tunid req_tun_ids << available_tun_id[i] i=i+1 end output_params = {:code => 200, :result => "get_tun_id2_1: Request completed", :body => {:selected_tun_ids => req_tun_ids}, :sdk => "freewifi"} rescue output_params = {:code => 500, :result => "get_tun_id2_1: Something Wrong", :sdk => "freewifi"} end additional_func_wifi_1.printer_texter_wifi_1({:input_params => input_params, :output_params => output_params, :procedure => "get_tun_id2_1"}, "debug") output_params end #universal procedure for get currently configured eoip tunnels def hub_get_all_eoip_tunnels_1(hub_ip_address, hub_login, hub_password) input_params = {:hub_ip_address => hub_ip_address, :hub_login => hub_login, :hub_password => hub_password} output_params = {} result = "" busy_tunnel_ids = [] begin ssh_connection = Net::SSH.start(hub_ip_address, hub_login, :password => hub_password) do |ssh| result = ssh.exec!("/interface eoip print where tunnel-id>=#{t_id_min.to_s} tunnel-id<=#{t_id_max.to_s}") end #p ssh_connection. reg_tunnelid = /tunnel-id=(\d+)/ m1 = result.scan(reg_tunnelid) for a1 in m1 if a1[0] != [] s1 = a1[0].to_i busy_tunnel_ids << s1 end end output_params = {:code => 200, :result => "hub_get_all_eoip_tunnels_1: Request completed", :body => {:busy_tunnel_ids => busy_tunnel_ids}, :sdk => "freewifi"} rescue output_params = {:code => 500, :result => "hub_get_all_eoip_tunnels_1: Something Wrong", :sdk => "freewifi"} end additional_func_wifi_1.printer_texter_wifi_1({:input_params => input_params, :output_params => output_params, :procedure => "hub_get_all_eoip_tunnels_1"}, "debug") output_params #m1 = reg_tunnelid.scan(result) #p busy_tunnel_ids #g = result.split("\r\n\r\n") #p g #result end def add_eoip_for_mikr_wifi_1(bridge_name, ap_ip_address, company_name, hub1, hub2, user_hub, password_hub) input_params = {:bridge_name => bridge_name, :ap_ip_address => ap_ip_address, :company_name => company_name, :hub1 => hub1, :hub2 => hub2, :user_hub => user_hub, :password_hub => password_hub } output_params = {} hub1_processed = "" hub2_processed = "" get_tun_id_answ = get_tun_id2_1(1, hub_get_all_eoip_tunnels_1(hub1, user_hub, password_hub)[:body][:busy_tunnel_ids]) eoip_tunnel_name = additional_func_wifi_1.hub_tunnelname_generator_1(get_tun_id_answ[:body][:selected_tun_ids][0], company_name) if get_tun_id_answ[:code] == 200 begin Net::SSH.start(hub1, user_hub, :password => password_hub) do |ssh| result = ssh.exec!("/interface eoip add remote-address=#{ap_ip_address} tunnel-id=#{get_tun_id_answ[:body][:selected_tun_ids][0]} name=#{eoip_tunnel_name}") result2 = ssh.exec!("/interface bridge port add bridge=#{bridge_name} interface=#{eoip_tunnel_name} unknown-multicast-flood=no unknown-unicast-flood=no") end hub1_processed = "Success" rescue hub1_processed = "Failed" end #Net::SSH.stop begin Net::SSH.start(hub2, user_hub, :password => password_hub) do |ssh| result = ssh.exec!("/interface eoip add remote-address=#{ap_ip_address} tunnel-id=#{get_tun_id_answ[:body][:selected_tun_ids][0]} name=#{eoip_tunnel_name}") result2 = ssh.exec!("/interface bridge port add bridge=#{bridge_name} interface=#{eoip_tunnel_name} unknown-multicast-flood=no unknown-unicast-flood=no") end #Net::SSH.stop hub2_processed = "Success" rescue hub2_processed = "Failed" end output_params = {:code => 200, :result => "add_eoip_for_mikr_wifi_1: Request completed", :body => {:eoip_tunnel_id => get_tun_id_answ[:body][:selected_tun_ids][0], :eoip_tunnel_name => eoip_tunnel_name, :hub1_processed => hub1_processed, :hub2_processed => hub2_processed}} else output_params = {:code => 500, :result => "add_eoip_for_mikr_wifi_1: Something Wrong with empty eoip tun id", :sdk => "freewifi"} end additional_func_wifi_1.printer_texter_wifi_1({:input_params => input_params, :output_params => output_params, :procedure => "add_eoip_for_mikr_wifi_1", :sdk => "freewifi"}, "debug") output_params end def mikr_tunnelid_gen() t_id_min = 10001 t_id_max = 65535 end def check_pool_range_includes_1(range, ip_addr_new) input_params ={:range => range, :ip_addr_new => ip_addr_new} output_params = {} begin res = "nothing" gs = range.split(",") for j in gs if j.include?("/") ipaddr1 = IPAddr.new ip_addr_new x1 = j.split("/") ipaddr2 = ipaddr1.mask(x1[1].to_i).to_s ipaddr3 = ipaddr2.to_s if ipaddr3 == x1[0] res = "exists" end else if j == ip_addr_new res = "exists" end end end output_params = {:code => 200, :result => "check_pool_range_includes_1: Request processed", :body => {:result => res}} rescue output_params = {:code => 507, :result => "check_pool_range_includes_1: Unknown SDK error"} end additional_func_wifi_1.printer_texter_wifi_1({:input_params => input_params, :output_params => output_params, :procedure => "check_pool_range_includes_1", :file => "freewifi/mikrotik/newhub_mtik_ssh.rb", :sdk => "freewifi"}, "debug") output_params end def capsman_set_ip_range_1(capsman1, capsman_user1, capsman_pass1, ap_ip) end def capsman_new_configuration_1(wifissid, capsman1, capsman_user1, capsman_pass1, ap_ip, companyname) conf_24ghz_name = "auto_cfg_24ghz_#{companyname}_#{wifissid}" command_conf_24ghz = "/caps-man configuration add name=#{conf_24ghz_name} ssid=#{wifissid} country=belarus mode=ap hw-retries=15 channel=24_channel_24GHz_dynamic_power22 datapath=1_datapath_MTS_Local-forwarding security=security_without_passphrase_default_FREE-WiFi" conf_5ghz_name = "auto_cfg_5ghz_#{companyname}_#{wifissid}" command_conf_5ghz = "/caps-man configuration add name=#{conf_5ghz_name} ssid=#{wifissid} country=belarus mode=ap hw-retries=15 channel=5_channel_5GHz_number1_power23 datapath=1_datapath_MTS_Local-forwarding security=security_without_passphrase_default_FREE-WiFi" command_provisioning_24ghz = "/caps-man provisioning add hw-supported-modes=b,g,gn ip-address-ranges=#{ap_ip} action=create-dynamic-enabled place-before=30 master-configuration=#{conf_24ghz_name} name-format=identity" command_provisioning_5ghz = "/caps-man provisioning add hw-supported-modes=a,ac,an ip-address-ranges=#{ap_ip} action=create-dynamic-enabled place-before=30 master-configuration=#{conf_24ghz_name} name-format=identity" Net::SSH.start(capsman1, capsman_user1, :password => capsman_pass1) do |ssh| result = ssh.exec!(command_conf_24ghz) result1 = ssh.exec!(command_conf_5ghz) result2 = ssh.exec!(command_provisioning_24ghz) result3 = ssh.exec!(command_provisioning_5ghz) end end def capsman_add_ip_to_existed_1(ap_name_list, capsman1, capsman_user1, capsman_pass1, ap_ip) command = "/caps-man provisioning print where " for h in ap_name_list command = command + " or master-configuration=" + h end command = command.gsub("where or", "where") final_provision_old = [] Net::SSH.start(capsman1, capsman_user1, :password => capsman_pass1) do |ssh| result = ssh.exec!(command) s1 = result.split("\r\n\r\n") for a in s1 a1 = a.gsub("\r\n", "") a2 = a1.gsub(", ", ",") reg_addr_range = / (\d) .+ ip-address-ranges=(\d{1,3}.+\.\d{1,3}|\d{1,3}.+\.\d{1,3}\/\d{1,2})\s+/ m1 = a2.scan(reg_addr_range) num = m1[0][0] ip_ran = m1[0][1].gsub(" ", "") triplet = [num, ip_ran] final_provision_old << triplet exists_check = self.check_pool_range_includes_1(triplet[1], ap_ip) if exists_check[:body][:result] != "exists" #self.capsman_set_ip_range_1(capsman1, capsman_user1, capsman_pass1, ap_ip) #set data to capsman rewrite_ip_command = "/caps-man provisioning set numbers=#{triplet[0]} ip-address-ranges=#{triplet[1]},#{ap_ip}" p rewrite_ip_command ssh.exec!(rewrite_ip_command) end end end end def set_capsman_config_1(wifissid, capsman1, capsman_user1, capsman_pass1, ap_ip, companyname) input_params ={:wifissid => wifissid, :capsman1 => capsman1, :capsman_user1 => capsman_user1, :capsman_pass1 => capsman_pass1, :ap_ip => ap_ip, :companyname => companyname } output_params = {} begin Net::SSH.start(capsman1, capsman_user1, :password => capsman_pass1) do |ssh| result = ssh.exec!("/caps-man configuration print where ssid=#{wifissid}") if result == "\r\n" || result == "" || result == nil self.capsman_new_configuration_1(wifissid, capsman1, capsman_user1, capsman_pass1, ap_ip, companyname) else p result conf_names = [] reg_config_name = /name=\"(\S+)\"/ m1 = result.scan(reg_config_name) for a1 in m1 if a1[0] != [] s1 = a1[0] conf_names << s1 end end self.capsman_add_ip_to_existed_1(conf_names, capsman1, capsman_user1, capsman_pass1, ap_ip) end end rescue output_params = {:code => 507, :result => "set_capsman_config_1: Unknown SDK error"} end additional_func_wifi_1.printer_texter_wifi_1({:input_params => input_params, :output_params => output_params, :procedure => "set_capsman_config_1", :file => "freewifi/mikrotik/newhub_mtik_ssh.rb", :sdk => "freewifi"}, "debug") output_params end end