First pass at serializing records from -ltnp output
parent
e250e45c46
commit
a463ec305b
@ -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
|
@ -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
|
@ -0,0 +1,8 @@
|
||||
require "../modules/record_helpers"
|
||||
|
||||
class LtnpRecord
|
||||
def initialize(record : String)
|
||||
@str_arr = RecordHelpers.clean_record(record)
|
||||
|
||||
end
|
||||
end
|
@ -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
|
@ -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
|
@ -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
|
Loading…
Reference in New Issue