From 678a3c39f87cf44193f96641cd64b7e995b6ec49 Mon Sep 17 00:00:00 2001 From: BraydonKains Date: Fri, 1 May 2020 21:35:25 -0400 Subject: [PATCH 1/3] Added ability to kill process found on port --- main.cr | 26 +++++++++++++++++++++----- modules/command_runner.cr | 13 +++++++++++++ modules/netstat_runner.cr | 7 ------- 3 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 modules/command_runner.cr delete mode 100644 modules/netstat_runner.cr 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 From c980c448f8b340f37ed6860c22ba09d2fddf7090 Mon Sep 17 00:00:00 2001 From: BraydonKains Date: Fri, 1 May 2020 21:50:37 -0400 Subject: [PATCH 2/3] Gonna output the process IDs too --- main.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.cr b/main.cr index b7f976c..7f1a12f 100644 --- a/main.cr +++ b/main.cr @@ -20,7 +20,7 @@ OptionParser.parse do |parser| record = ltnp_operator.get_port_record port unless record.nil? - puts "Port: %s, Process: %s" % [record.port, record.p_name] + puts "Port: %s, Process: %s, Process ID: %s" % [record.port, record.p_name, record.pid] else puts "No processes found on port %s" % port end @@ -33,7 +33,7 @@ OptionParser.parse do |parser| record = ltnp_operator.get_port_record port unless record.nil? - puts "Port: %s, Process: %s" % [record.port, record.p_name] + puts "Port: %s, Process: %s, Process ID: %s" % [record.port, record.p_name, record.pid] puts "Would you like to kill %s? (y/n)" % [record.p_name] input = gets unless input.nil? From 5f5362f3ba0e5090266b406089d6c9c03c753e76 Mon Sep 17 00:00:00 2001 From: BraydonKains Date: Fri, 1 May 2020 22:09:17 -0400 Subject: [PATCH 3/3] readme updated --- README.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d47faea..b4b5d5d 100644 --- a/README.md +++ b/README.md @@ -13,16 +13,18 @@ Show help menu. ### -c PORT, --check-port=PORT Get the name and process ID of the process running on a given port +### -k PORT, --kill-port=PORT +Check the process running on a given port with the option to kill it if you'd like. + ## TODO -- [] Add the ability to kill process on port -- [] Maybe look into other cli operations? (getting IP?) -- [] Come up with other useful stuff -- [] Write up contributors guide -- [] Decide how to do documentation (probably Github wiki?) -- [] Write man page (figure out how to do that lol) -- [] Figure out distribution/release - -- [x] Set up CI (probably Travis) +* [x] Add the ability to kill process on port +* [] Maybe look into other cli operations? (getting IP?) +* [] Come up with other useful stuff +* [] Write up contributors guide +* [] Decide how to do documentation (probably Github wiki?) +* [] Write man page (figure out how to do that lol) +* [] Figure out distribution/release +* [x] Set up CI (probably Travis) ## Contributing I don't know that I'm ready for contributors yet because I don't have a good contributors guide. I do plan to eventually open this up to contributors. If you have any ideas for future operations, please feel free to open an issue, I am happy to discuss ideas!