Functionality for eznet to check process on port
parent
a463ec305b
commit
3bbf0bba27
@ -0,0 +1,2 @@
|
|||||||
|
*.swp
|
||||||
|
eznet
|
@ -1,16 +1,18 @@
|
|||||||
require "./ltnp_record"
|
require "./ltnp_record"
|
||||||
|
|
||||||
class LtnpOperator
|
class LtnpOperator
|
||||||
def initialize(_cmd_output : String)
|
def initialize(cmd_output : String)
|
||||||
@cmd_output = _cmd_output
|
records = cmd_output.each_line
|
||||||
records = @cmd_output.each_line
|
|
||||||
records = records.each.select(/^tcp/)
|
records = records.each.select(/^tcp/)
|
||||||
@ltnp_records = Array(LtnpRecord).new
|
@ltnp_records = Array(LtnpRecord).new
|
||||||
records.each { |s| @ltnp_records << LtnpRecord.new s }
|
records.each { |s| @ltnp_records << LtnpRecord.new s }
|
||||||
end
|
end
|
||||||
|
|
||||||
def say_hi
|
def say_hi
|
||||||
puts @cmd_output
|
@ltnp_records.each { |r| puts r.to_s }
|
||||||
puts @ltnp_records.to_s
|
end
|
||||||
|
|
||||||
|
def get_port_record(port : String) : LtnpRecord | Nil
|
||||||
|
@ltnp_records.find { |r| r.port == port }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,8 +1,53 @@
|
|||||||
require "../modules/record_helpers"
|
require "../modules/record_helpers"
|
||||||
|
|
||||||
class LtnpRecord
|
class LtnpRecord
|
||||||
|
getter proto : String
|
||||||
|
getter state : String
|
||||||
|
getter address : String
|
||||||
|
getter port : String
|
||||||
|
getter f_address : String
|
||||||
|
getter f_port : String
|
||||||
|
getter state : String
|
||||||
|
getter pid : String
|
||||||
|
getter p_name : String
|
||||||
|
|
||||||
def initialize(record : String)
|
def initialize(record : String)
|
||||||
@str_arr = RecordHelpers.clean_record(record)
|
ser_record = RecordHelpers.clean_record(record)
|
||||||
|
@proto = ser_record[0]
|
||||||
|
@address, @port = ser_address_port ser_record[3].split(':')
|
||||||
|
@f_address, @f_port = ser_address_port ser_record[4].split(':')
|
||||||
|
@state = ser_record[5]
|
||||||
|
@pid, @p_name = ser_pid_p_name ser_record[6]
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
puts "%s, " * 8 % [
|
||||||
|
@proto, @state, @address, @port,
|
||||||
|
@f_address, @f_port, @pid, @p_name
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
def ser_address_port(address_port : Array) : {String, String}
|
||||||
|
address = String.new
|
||||||
|
port = String.new
|
||||||
|
unless address_port.size > 2
|
||||||
|
address = address_port[0]
|
||||||
|
port = address_port[1]
|
||||||
|
else
|
||||||
|
port = address_port[-1]
|
||||||
|
end
|
||||||
|
{address, port}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def ser_pid_p_name(pid_pro_str : String) : {String, String}
|
||||||
|
pid = String.new
|
||||||
|
p_name = String.new
|
||||||
|
unless pid_pro_str == "-"
|
||||||
|
pid_pro = pid_pro_str.split('/')
|
||||||
|
pid = pid_pro[0]
|
||||||
|
p_name = pid_pro[1]
|
||||||
|
end
|
||||||
|
{pid, p_name}
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
module NetstatRunner
|
module NetstatRunner
|
||||||
def self.run_ltnp()
|
def self.run_ltnp() : LtnpOperator
|
||||||
io = IO::Memory.new
|
io = IO::Memory.new
|
||||||
Process.run("netstat -ltnp", shell: true, output: io)
|
Process.run("sudo netstat -ltnp", shell: true, output: io)
|
||||||
io.to_s
|
LtnpOperator.new io.to_s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
require "spec"
|
||||||
|
require "../../ltnp/ltnp_operator.cr"
|
||||||
|
|
||||||
|
describe LtnpOperator do
|
||||||
|
sut = LtnpOperator.new File.read("./spec/input/ltnp_example.txt")
|
||||||
|
|
||||||
|
describe "#get_port_record" do
|
||||||
|
existing_port = "8081"
|
||||||
|
nonsense_port = "1234"
|
||||||
|
|
||||||
|
it "should find record when port exists" do
|
||||||
|
sut.get_port_record(existing_port).nil?.should be_false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not find record when port doesn't exist" do
|
||||||
|
sut.get_port_record(nonsense_port).nil?.should be_true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,29 @@
|
|||||||
|
require "spec"
|
||||||
|
require "../../ltnp/ltnp_record.cr"
|
||||||
|
|
||||||
|
describe LtnpRecord do
|
||||||
|
sut_normal = LtnpRecord.new "tcp 0 0 127.0.0.1:8081 0.0.0.0:* LISTEN 69/myproc"
|
||||||
|
sut_address = LtnpRecord.new "tcp 0 0 :::8081 0.0.0.0:* LISTEN 69/myproc"
|
||||||
|
sut_process = LtnpRecord.new "tcp 0 0 :::8081 0.0.0.0:* LISTEN -"
|
||||||
|
|
||||||
|
it "should have expected properties under normal circumstance" do
|
||||||
|
sut_normal.proto.should eq("tcp")
|
||||||
|
sut_normal.address.should eq("127.0.0.1")
|
||||||
|
sut_normal.port.should eq("8081")
|
||||||
|
sut_normal.f_address.should eq("0.0.0.0")
|
||||||
|
sut_normal.f_port.should eq("*")
|
||||||
|
sut_normal.state.should eq("LISTEN")
|
||||||
|
sut_normal.pid.should eq("69")
|
||||||
|
sut_normal.p_name.should eq("myproc")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have expected properties when Local Address column is edge case" do
|
||||||
|
sut_address.address.empty?.should be_true
|
||||||
|
sut_address.port.should eq("8081")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have expected properties when PID/ProcessName column is edge case" do
|
||||||
|
sut_process.pid.empty?.should be_true
|
||||||
|
sut_process.p_name.empty?.should be_true
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue