Class: Tnnl::Process
- Inherits:
-
Object
- Object
- Tnnl::Process
- Defined in:
- lib/tnnl/process.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#pid ⇒ Object
Returns the value of attribute pid.
Class Method Summary collapse
- .kill_all ⇒ Object
- .kill_several(*to_kill) ⇒ Object
-
.list ⇒ Object
Returns an array of instances of Tnnl::Process representing each of the currently open SSH tunnels created by Tnnl on this machine.
Instance Method Summary collapse
-
#initialize(pid, name) ⇒ Process
constructor
A new instance of Process.
- #kill ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(pid, name) ⇒ Process
Returns a new instance of Process.
36 37 38 39 |
# File 'lib/tnnl/process.rb', line 36 def initialize(pid, name) @pid = pid.to_i @name = name end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/tnnl/process.rb', line 3 def name @name end |
#pid ⇒ Object
Returns the value of attribute pid.
3 4 5 |
# File 'lib/tnnl/process.rb', line 3 def pid @pid end |
Class Method Details
.kill_all ⇒ Object
30 31 32 |
# File 'lib/tnnl/process.rb', line 30 def kill_all list.each(&:kill) end |
.kill_several(*to_kill) ⇒ Object
24 25 26 27 28 |
# File 'lib/tnnl/process.rb', line 24 def kill_several(*to_kill) list.each_with_index do |process, i| process.kill if to_kill.include?(i+1) end end |
.list ⇒ Object
Returns an array of instances of Tnnl::Process representing each of the currently open SSH tunnels created by Tnnl on this machine.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/tnnl/process.rb', line 8 def list # Grep the process list to get processes with "tnnl" somewhere in the # name. Format the output from ps for easier parsing. list = `ps -eo pid,comm | grep tnnl`.split(/\n/) # Transform each string into an instance of Tnnl::Process. processes = list.map do |line| pid, name = line.strip.split(' ') self.new(pid, name) end # Remove any processes we might have found that don't conform to our # naming convention. processes.select { |p| p.name =~ /^tnnl\[.*\]$/ } end |
Instance Method Details
#kill ⇒ Object
41 42 43 |
# File 'lib/tnnl/process.rb', line 41 def kill ::Process.kill('INT', pid) end |
#to_s ⇒ Object
45 46 47 48 49 |
# File 'lib/tnnl/process.rb', line 45 def to_s = name.scan(/\[(.*)\]/).last.first local_port, host, remote_port = .split(':') "localhost:#{local_port} ==> #{host}:#{remote_port}" end |