genki-ns 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/ns.rb +10 -30
  2. metadata +4 -3
data/lib/ns.rb CHANGED
@@ -3,9 +3,7 @@ require 'singleton'
3
3
  require 'socket'
4
4
 
5
5
  class Ns
6
- VERSION = "0.0.3"
7
- Listener = Struct.new(:proto, :recvq, :sendq, :laddr, :raddr, :state, :prog)
8
- @@all = false
6
+ VERSION = "0.1.0"
9
7
 
10
8
  include Singleton
11
9
 
@@ -14,43 +12,25 @@ class Ns
14
12
  action = :list
15
13
  opt = OptionParser.new
16
14
  opt.on('-k', 'kill'){action = :kill}
17
- opt.on('-a', 'all'){@@all = true}
18
15
  opt.order!(ARGV)
19
16
  instance.send action, ARGV
20
17
  end
21
18
  end
22
19
 
23
20
  def list(ports)
24
- @listeners.each do |l|
25
- unless ports.empty?
26
- next unless l.laddr.match(/[^\:]\:(?:#{ports.join('|')})\b/)
27
- end
28
- next if !@@all && (l.prog.nil? || l.prog == '-')
29
- next if l.laddr.split(':').size > 2
30
- puts "%18s %18s %s" % [l.laddr, l.raddr, l.prog]
31
- end
21
+ system 'lsof', *ports.map{|i| "-i:#{i}"}
32
22
  end
33
23
 
34
24
  def kill(ports)
35
25
  puts "ports required" or exit if ports.empty?
36
- @listeners.each do |l|
37
- unless ports.empty?
38
- next unless l.laddr.match(/[^\:]\:(?:#{ports.join('|')})\b/)
26
+ lines = `lsof#{ports.map{|i| " -i:#{i}"}}`.split("\n")[1..-1]
27
+ lines.each do |line|
28
+ pid = line.split(/\s+/)[1]
29
+ if system "kill -#{@signal || '9'} #{pid}"
30
+ puts "#{pid} killed."
31
+ else
32
+ puts "#{pid} not killed."
39
33
  end
40
- next if l.laddr.split(':').size > 2
41
- next if l.prog.nil?
42
- pid, progname = l.prog.split('/', 2)
43
- system "kill -9 #{pid}"
44
- puts "%s(%s) killed" % [progname, pid]
45
- end
46
- end
47
-
48
- private
49
- def initialize
50
- @listeners = []
51
- lines = `netstat -n -l -p 2>/dev/null`.split("\n")[1..-1]
52
- lines.grep(/^(tcp|udp)/).each do |line|
53
- @listeners << Listener.new(*line.split(/\s+/))
54
- end
34
+ end unless lines.nil?
55
35
  end
56
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genki-ns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Takiuchi
@@ -9,11 +9,12 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-12 00:00:00 -07:00
12
+ date: 2009-02-18 00:00:00 -08:00
13
13
  default_executable: ns
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: redgreen
17
+ type: :runtime
17
18
  version_requirement:
18
19
  version_requirements: !ruby/object:Gem::Requirement
19
20
  requirements:
@@ -35,8 +36,8 @@ files:
35
36
  - ChangeLog
36
37
  - Rakefile
37
38
  - bin/ns
38
- - test/test_helper.rb
39
39
  - test/ns_test.rb
40
+ - test/test_helper.rb
40
41
  - lib/ns.rb
41
42
  has_rdoc: true
42
43
  homepage: http://ns.rubyforge.org