oxidized icon indicating copy to clipboard operation
oxidized copied to clipboard

Tp-link change backup

Open re2play opened this issue 1 year ago • 1 comments

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 avatar Jun 14 '24 06:06 re2play

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 avatar Jun 19 '24 11:06 re2play

Hello, any idea yet?

re2play avatar Jul 11 '24 06:07 re2play

make this config, maybe not correct, but work 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 redundant prompt lines cfg.gsub! /^([\w.@()-]+[#>])$/, '' # remove empty lines cfg.each_line.reject { |line| line.strip.empty? }.join end

cmd :secret do |cfg| cfg.gsub! /^enable password (\S+)/, 'enable password

cmd "show system-info" do |cfg| cfg.gsub! /(System Time\s+-)./, '\1 ' cfg.gsub! /(Running Time\s+-)./, '\1 ' 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 avatar Aug 01 '24 07:08 re2play

thanks for help

re2play avatar Aug 01 '24 07:08 re2play

I try your config but not work for me. Model TP-Link is SG3210 3.20 PS: from oxidized log i have informations as "show system-info" and "show running-config" are executed but with not success output and I got "

Error: Bad command " PS: as if command "enable" is not used after login

GeComDohlad avatar Nov 14 '24 12:11 GeComDohlad