Advertisement
blotavious

Untitled

Jan 14th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.76 KB | None | 0 0
  1. require 'net/telnet'
  2.  
  3. class SDTDSay
  4.  
  5.    def initialize(options)
  6.       @options = options
  7.       if @options[:device] == 'STDOUT' then
  8.          @device = STDOUT
  9.       else
  10.          @device = Net::Telnet::new('Host' => options[:host],
  11.                                     'Port' => options[:port],
  12.                                     'Telnetmode' => true,
  13.                                     'Timeout' => 60,
  14.                                     'Prompt' => /\n/)
  15.          @device.cmd(options[:password])
  16.       end
  17.    end
  18.  
  19.    def send(line)
  20.       if @device.class == STDOUT.class then
  21.          @device.puts "#{self.fix_msg(line)}"
  22.       else
  23.          @device.cmd("#{self.fix_msg(line)}") do |resp|
  24.             puts "#{resp}" unless @options[:quiet]
  25.          end
  26.       end
  27.    end
  28.  
  29.    def close
  30.       @device.puts('exit') unless @device.class == STDOUT.class
  31.       @device.close unless @device.class == STDOUT.class
  32.    end
  33.  
  34.    def fix_msg(msg)
  35.       # _GREEN_ translated to color [00FFCC]
  36.       # _RED_ translated to color [FF3300]
  37.       # _WHITE_ translated to color [FFFFFF]
  38.       # _YELLOW_ translated to color [FFFF99]
  39.       # _BLUE_ translated to color [00CCFF]
  40.       # _NOCOLOR_ translated to color [-]
  41.  
  42.       if @device.class == STDOUT.class then
  43.          msg.gsub!(/_GREEN_/, '')
  44.          msg.gsub!(/_RED_/, '')
  45.          msg.gsub!(/_WHITE_/, '')
  46.          msg.gsub!(/_YELLOW_/, '')
  47.          msg.gsub!(/_BLUE_/, '')
  48.          msg.gsub!(/_NOCOLOR_/, '')
  49.       else
  50.          msg.gsub!(/_GREEN_/, '[00FFCC]')
  51.          msg.gsub!(/_RED_/, '[FF3300]')
  52.          msg.gsub!(/_WHITE_/, '[FFFFFF]')
  53.          msg.gsub!(/_YELLOW_/, '[FFFF99]')
  54.          msg.gsub!(/_BLUE_/, '[00CCFF]')
  55.          msg.gsub!(/_NOCOLOR_/, '[-]')
  56.       end
  57.  
  58.       return msg
  59.    end
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement