<commit_before># == Schema Information
#
# Table name: nodes
#
#  id          :integer          not null, primary key
#  name        :string
#  ip          :string
#  location_id :integer
#  type        :string
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#  model       :string
#  fttb        :boolean          default(FALSE)
#

class Zte < Node
  require 'net/telnet'

  def get_ports
    if self.model =~ /2936/ && self.ip != "10.168.253.60"
      super
    else
      port_number = self.model.match(/.*(\d{2})/)[1]
      zte_model = self.model.match(/(?<=ZXR10)-*(\d{4})/)[1]
      @ports_info_arr = []
      host = self.ip
      begin
        tn = Net::Telnet::new(
          "Host" => host,
          "Timeout" => 1,
          "Waittime" => 0.5,
          "Prompt" => /[#>:]/n,
          "Output_log" => "output_log.txt"
        )
        tn.waitfor(/:/) do |banner|
          zte_model_regex = Regexp.new("#{zte_model}|ZTE")
          if banner.match(zte_model_regex)
            tn.cmd("#{ENV["ZTE_USERNAME"]}\n#{ENV["ZTE_PASSWORD"]}") #{ |c| print c }
            port_number.to_i.times do |i|
              port = zte_model =~ /5250/ ? "1/#{i+1}" : i+1
              tn.cmd("show port #{port}")
              tn.cmd(" ")
            end
            tn.cmd("n")
            tn.cmd("n")
            tn.cmd("exit")
          end
        end
        tn.close

        @ports_info_arr = parse_log("output_log.txt")

      rescue Exception => e  
        puts e.message  
        puts e.backtrace.inspect
      ensure
        # Remove unnecessary log file
        system('rm output_log.txt')
      end
      @ports_info_arr
    end
  end

  # Parsing log file
  def parse_log(file)
    lines = []
    if File.exist?(file)
      File.open(file, "r") do |f|
        f.each do |line|
          case line
            when /PortId|Description|PortName|PortEnable/
              lines << line.match(/([A-Z]\w+(?= *:)) *: (\S+)\s/)[2]
            when /Link/
              lines << line.match(/(?<=Link           : )(\w+)/)[1]
          end
        end
      end
    end
    lines
        .slice_before(/\A1*\/*\d{1,2}\z/)
        .map do |id, description = "", port_enable, link|
          state = port_enable == "enabled" ? link : "admin down"
          {name: id, description: description, state: state}
        end
  end
end<commit_msg>Uses variable to get telnet output<commit_after># == Schema Information
#
# Table name: nodes
#
#  id          :integer          not null, primary key
#  name        :string
#  ip          :string
#  location_id :integer
#  type        :string
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#  model       :string
#  fttb        :boolean          default(FALSE)
#

class Zte < Node
  require 'net/telnet'

  def get_ports
    if self.model =~ /2936/ && self.ip != "10.168.253.60"
      super
    else
      port_number = self.model.match(/.*(\d{2})/)[1]
      zte_model = self.model.match(/(?<=ZXR10)-*(\d{4})/)[1]
      @ports_info_arr = []
      host = self.ip
      begin
        tn = Net::Telnet::new(
          "Host" => host,
          "Timeout" => 1,
          "Waittime" => 0.5,
          "Prompt" => /[#>:]/n#,
          # "Output_log" => "output_log.txt"
        )
        tn.waitfor(/:/) do |banner|
          zte_model_regex = Regexp.new("#{zte_model}|ZTE")
          if banner.match(zte_model_regex)
            response = []
            tn.cmd("#{ENV["ZTE_USERNAME"]}\n#{ENV["ZTE_PASSWORD"]}") #{ |c| print c }
            port_number.to_i.times do |i|
              port = zte_model =~ /5250/ ? "1/#{i+1}" : i+1
              response << tn.cmd("show port #{port}")
              response << tn.cmd(" ")
            end
            response << tn.cmd("n")
            response << tn.cmd("n")
            response << tn.cmd("exit")
          end
        end
        tn.close

        # @ports_info_arr = parse_log("output_log.txt")

      rescue Exception => e  
        puts e.message  
        puts e.backtrace.inspect
      ensure
        # Remove unnecessary log file
        # system('rm output_log.txt')
      end
      @ports_info_arr
    end
  end

  # Parsing log file
  def parse_log(response)
  # def parse_log(file)
    lines = []
    # if File.exist?(file)
    #   File.open(file, "r") do |f|
    response
        .split("\n")
        .reject(&:empty?)
        .each do |line|
          case line
            when /PortId|Description|PortName|PortEnable/
              lines << line.match(/([A-Z]\w+(?= *:)) *: (\S+)\s/)[2]
            when /Link/
              lines << line.match(/(?<=Link           : )(\w+)/)[1]
          end
        end
    #   end
    # end
    lines
        .slice_before(/\A1*\/*\d{1,2}\z/)
        .map do |id, description = "", port_enable, link|
          state = port_enable == "enabled" ? link : "admin down"
          {name: id, description: description, state: state}
        end
  end
end
{'fraction_non_alphanumeric': 0.15440435396089497, 'fraction_numerical': 0.01411005845595646, 'mean_word_length': 1.5803432137285491, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '608454', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1675, 'n_words': 357}