Class: Exec

Inherits:
Object
  • Object
show all
Defined in:
lib/exec.rb

Direct Known Subclasses

Netcat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, **options) ⇒ Exec

Returns a new instance of Exec.



8
9
10
11
12
13
14
15
# File 'lib/exec.rb', line 8

def initialize(cmd, **options)
    handle_exception
    @i, @o, s = Open3.popen2(cmd)
    # Debug msg
    @debug = (options.has_key? :debug) ? options[:debug] : true
    # Log color
    Rainbow.enabled = false if options[:color] == false
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



5
6
7
# File 'lib/exec.rb', line 5

def color
  @color
end

#debugObject

Returns the value of attribute debug.



5
6
7
# File 'lib/exec.rb', line 5

def debug
  @debug
end

Instance Method Details

#getsObject



35
36
37
# File 'lib/exec.rb', line 35

def gets
    read_until "\n"
end

#interactiveObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/exec.rb', line 50

def interactive
    loop do
        r = IO.select [@o, $stdin]
        if r[0].include? @o
            read 1
        elsif r[0].include? $stdin
            @i.write $stdin.read(1)
        end
    end
end

#puts(data) ⇒ Object



31
32
33
# File 'lib/exec.rb', line 31

def puts(data)
    write "#{data}\n"
end

#read(size) ⇒ Object



17
18
19
# File 'lib/exec.rb', line 17

def read(size)
    @o.read(size).tap {|data| write_flush $stdout, data.color(:cyan) if @debug}
end

#read_until(str) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/exec.rb', line 39

def read_until(str)
    result = ""
    loop do
        result << @o.read(1)
        if result.end_with? str
            write_flush $stdout, result.color(:cyan) if @debug
            return result
        end
    end
end

#readpartial(size) ⇒ Object



21
22
23
# File 'lib/exec.rb', line 21

def readpartial(size)
    @o.readpartial(size).tap {|data| write_flush $stdout, data.color(:cyan) if @debug}
end

#write(data) ⇒ Object



25
26
27
28
29
# File 'lib/exec.rb', line 25

def write(data)
    data = data.to_s if data.is_a? Integer
    write_flush $stdout, data.color(:yellow) if @debug
    write_flush @i, data
end