ytti / oxidized

Oxidized is a network device configuration backup tool. It's a RANCID replacement!
Apache License 2.0
2.7k stars 911 forks source link

Tp-link change backup #3191

Open re2play opened 1 month ago

re2play commented 1 month ago

I full reading and doing ticket #730 and #1608, but my backup from tplink TL-SG3452X random add/del some string image image image

my tplink.rb

class TPLink < Oxidized::Model
  using Refinements

  # tp-link prompt
  prompt /^\r?([\w.@()-]+[#>]\s?)$/
  comment '! '

  def ssh
    @input.class.to_s.match(/SSH/)
  end

  # handle paging
  # workaround for sometimes missing whitespaces with "\s?"
  expect /Press\s?any\s?key\s?to\s?continue\s?\(Q\s?to\s?quit\)/ do |data, re|
    send ' '
    data.sub re, ''
  end

  # send carriage return because \n with the command is not enough
  # checks if line ends with prompt >,# or \r,\nm otherwise send \r
  expect /[^>#\r\n:]$/ do |data, re|
    send "\r" if ssh
    data.sub re, ''
  end

  cmd :all do |cfg|
    # remove unwanted paging line
    cfg.gsub! /^Press any key to contin.*/, ''
    # normalize linefeeds
    cfg.gsub! /(\r|\r\n|\n\r)/, "\n"
    # remove empty lines
    cfg.each_line.reject { |line| line.match /^[\r\n\s\u0000#]+$/ }.join
  end

  cmd :secret do |cfg|
    cfg.gsub! /^enable password (\S+)/, 'enable password <secret hidden>'
    cfg.gsub! /^user (\S+) password (\S+) (.*)/, 'user \1 password <secret hidden> \3'
    cfg.gsub! /^(snmp-server community).*/, '\\1 <configuration removed>'
    cfg.gsub! /secret (\d+) (\S+).*/, '<secret hidden>'
    cfg
  end

  cmd "show system-info" do |cfg|
    cfg.gsub! /(System Time\s+-).*/, '\\1 <stripped>'
    cfg.gsub! /(Running Time\s+-).*/, '\\1 <stripped>'
    comment cfg.each_line.to_a[3..-3].join
  end

  cmd "show running-config" do |cfg|
    lines = cfg.each_line.to_a[1..-1]
    # cut config after "end"
    lines[0..lines.index("end\n")].join
  end

  cfg :telnet, :ssh do
    username /^(User ?[nN]ame|User):/
    password /^\r?[pP]assword:/
  end

  cfg :telnet, :ssh do
    post_login do
      if vars(:enable) == true
        cmd "enable"
      elsif vars(:enable)
        cmd "enable", /^[pP]assword:/
        send vars(:enable) + "\n\r"
      end
      cmd "terminal length 0"
    end

    pre_logout do
      send "exit\r"
      send "logout\r"
    end
  end
end
re2play commented 4 weeks ago

more detail about model switch CO-SW131#show system-info System Description - JetStream 48-Port Gigabit L2+ Managed Switch with 4 10GE SFP+ Slots System Name - KRDR-CO-SW131 System Location - Hong Kong Contact Information - www.tp-link.com Hardware Version - TL-SG3452X 1.0 Software Version - 1.0.6 Build 20230702 Rel.70603 Bootloader Version - TP-LINK BOOTUTIL(v1.0.0) Mac Address - 9C-A2-F4-00-00-00 Serial Number - 0000000000 System Time - 2024-06-19 14:19:28 Running Time - 143 day - 7 hour - 1 min - 25 sec

re2play commented 6 days ago

Hello, any idea yet?