diff --git a/main.cr b/main.cr index d0db420..b7f976c 100644 --- a/main.cr +++ b/main.cr @@ -1,6 +1,6 @@ require "option_parser" require "./ltnp/ltnp_operator" -require "./modules/netstat_runner" +require "./modules/command_runner" OptionParser.parse do |parser| parser.banner = "Welcome to eznetstat!" @@ -16,19 +16,35 @@ OptionParser.parse do |parser| end 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 + unless record.nil? puts "Port: %s, Process: %s" % [record.port, record.p_name] else puts "No processes found on port %s" % port end + exit end - parser.on "-k", "--kill-port", "Kill process on port" do - ltnp_operator = NetstatRunner.run_ltnp - ltnp_operator.say_hi + parser.on "-k PORT", "--kill-port=PORT", "Kill process on port" do |port| + ltnp_operator = CommandRunner.run_ltnp + 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 end end diff --git a/modules/command_runner.cr b/modules/command_runner.cr new file mode 100644 index 0000000..5d7551c --- /dev/null +++ b/modules/command_runner.cr @@ -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 diff --git a/modules/netstat_runner.cr b/modules/netstat_runner.cr deleted file mode 100644 index c480665..0000000 --- a/modules/netstat_runner.cr +++ /dev/null @@ -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