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! diff --git a/main.cr b/main.cr index d0db420..7f1a12f 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] + 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 + 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, 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? + 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