diff --git a/ltnp/ltnp_example.txt b/ltnp/ltnp_example.txt new file mode 100644 index 0000000..418b75d --- /dev/null +++ b/ltnp/ltnp_example.txt @@ -0,0 +1,8 @@ +Active Internet connections (only servers) +Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name +tcp 0 0 127.0.0.1:8081 0.0.0.0:* LISTEN 166475/polymer +tcp 0 0 0.0.0.0:57621 0.0.0.0:* LISTEN 163077/spotify --fo +tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1286/dnsmasq +tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 35841/sshd +tcp 0 0 0.0.0.0:53323 0.0.0.0:* LISTEN 163077/spotify --fo +tcp6 0 0 :::22 :::* LISTEN 35841/sshd diff --git a/ltnp/ltnp_operator.cr b/ltnp/ltnp_operator.cr new file mode 100644 index 0000000..c800ab9 --- /dev/null +++ b/ltnp/ltnp_operator.cr @@ -0,0 +1,16 @@ +require "./ltnp_record" + +class LtnpOperator + def initialize(_cmd_output : String) + @cmd_output = _cmd_output + records = @cmd_output.each_line + records = records.each.select(/^tcp/) + @ltnp_records = Array(LtnpRecord).new + records.each { |s| @ltnp_records << LtnpRecord.new s } + end + + def say_hi + puts @cmd_output + puts @ltnp_records.to_s + end +end diff --git a/ltnp/ltnp_record.cr b/ltnp/ltnp_record.cr new file mode 100644 index 0000000..276d455 --- /dev/null +++ b/ltnp/ltnp_record.cr @@ -0,0 +1,8 @@ +require "../modules/record_helpers" + +class LtnpRecord + def initialize(record : String) + @str_arr = RecordHelpers.clean_record(record) + + end +end diff --git a/main b/main new file mode 100755 index 0000000..aa5e9a0 Binary files /dev/null and b/main differ diff --git a/main.cr b/main.cr new file mode 100644 index 0000000..f0920f5 --- /dev/null +++ b/main.cr @@ -0,0 +1,24 @@ +require "option_parser" +require "./ltnp/ltnp_operator" +require "./modules/netstat_runner" + +OptionParser.parse do |parser| + parser.banner = "Welcome to eznetstat!" + + parser.on "-v", "--version", "Show version" do + puts "0.1" + exit + end + + parser.on "-h", "--help", "Show help" do + puts parser + exit + end + + parser.on "-k", "--kill-port", "Kill process on port" do + output = NetstatRunner.run_ltnp + ltnp_parser = LtnpOperator.new output + ltnp_parser.say_hi + exit + end +end diff --git a/modules/netstat_runner.cr b/modules/netstat_runner.cr new file mode 100644 index 0000000..06a4aaa --- /dev/null +++ b/modules/netstat_runner.cr @@ -0,0 +1,7 @@ +module NetstatRunner + def self.run_ltnp() + io = IO::Memory.new + Process.run("netstat -ltnp", shell: true, output: io) + io.to_s + end +end diff --git a/modules/record_helpers.cr b/modules/record_helpers.cr new file mode 100644 index 0000000..aa187b2 --- /dev/null +++ b/modules/record_helpers.cr @@ -0,0 +1,7 @@ +module RecordHelpers + def self.clean_record(record : String) : Array(String) + tokenized_record = record.split(" ") + tokenized_record = tokenized_record.reject { |s| s == "" } + tokenized_record + end +end