Added ability to kill process found on port

pull/3/head
BraydonKains 5 years ago
parent afbe72fa42
commit 678a3c39f8

@ -1,6 +1,6 @@
require "option_parser" require "option_parser"
require "./ltnp/ltnp_operator" require "./ltnp/ltnp_operator"
require "./modules/netstat_runner" require "./modules/command_runner"
OptionParser.parse do |parser| OptionParser.parse do |parser|
parser.banner = "Welcome to eznetstat!" parser.banner = "Welcome to eznetstat!"
@ -16,19 +16,35 @@ OptionParser.parse do |parser|
end end
parser.on "-c PORT", "--check-port=PORT", "Get the process name and ID of process on port" do |port| parser.on "-c PORT", "--check-port=PORT", "Get the process name and ID of process on port" do |port|
ltnp_operator = NetstatRunner.run_ltnp ltnp_operator = CommandRunner.run_ltnp
record = ltnp_operator.get_port_record port record = ltnp_operator.get_port_record port
unless record.nil? unless record.nil?
puts "Port: %s, Process: %s" % [record.port, record.p_name] puts "Port: %s, Process: %s" % [record.port, record.p_name]
else else
puts "No processes found on port %s" % port puts "No processes found on port %s" % port
end end
exit exit
end end
parser.on "-k", "--kill-port", "Kill process on port" do parser.on "-k PORT", "--kill-port=PORT", "Kill process on port" do |port|
ltnp_operator = NetstatRunner.run_ltnp ltnp_operator = CommandRunner.run_ltnp
ltnp_operator.say_hi record = ltnp_operator.get_port_record port
unless record.nil?
puts "Port: %s, Process: %s" % [record.port, record.p_name]
puts "Would you like to kill %s? (y/n)" % [record.p_name]
input = gets
unless input.nil?
if input.downcase == "y"
CommandRunner.run_kill record.pid
end
end
else
puts "No processes found on port %s" % port
end
exit exit
end end
end end

@ -0,0 +1,13 @@
module CommandRunner
def self.run_ltnp : LtnpOperator
io = IO::Memory.new
Process.run("netstat -ltnp", shell: true, output: io)
LtnpOperator.new io.to_s
end
def self.run_kill(pid : String) : String
io = IO::Memory.new
Process.run("kill #{pid}", shell: true, output: io)
io.to_s
end
end

@ -1,7 +0,0 @@
module NetstatRunner
def self.run_ltnp : LtnpOperator
io = IO::Memory.new
Process.run("sudo netstat -ltnp", shell: true, output: io)
LtnpOperator.new io.to_s
end
end
Loading…
Cancel
Save