Merge pull request #2 from BraydonKains/UseInit

Using files from crystal init and reformatting everything to new edit…
pull/3/head
Braydon Kains 5 years ago committed by GitHub
commit 94a8fe6d81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,9 @@
root = true
[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

9
.gitignore vendored

@ -1,2 +1,9 @@
# vim files
*.swp
eznet
# stuff from crystal init
/docs/
/lib/
/bin/
/.shards/
*.dwarf

@ -0,0 +1,5 @@
language: crystal
script:
- crystal spec
- crystal tool format --check

@ -15,6 +15,7 @@ Get the name and process ID of the process running on a given port
## 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?)

@ -2,17 +2,17 @@ require "./ltnp_record"
class LtnpOperator
def initialize(cmd_output : String)
records = cmd_output.each_line
records = records.each.select(/^tcp/)
@ltnp_records = Array(LtnpRecord).new
records.each { |s| @ltnp_records << LtnpRecord.new s }
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
@ltnp_records.each { |r| puts r.to_s }
@ltnp_records.each { |r| puts r.to_s }
end
def get_port_record(port : String) : LtnpRecord | Nil
@ltnp_records.find { |r| r.port == port }
@ltnp_records.find { |r| r.port == port }
end
end

@ -12,42 +12,41 @@ class LtnpRecord
getter p_name : String
def initialize(record : String)
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]
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
]
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}
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
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}
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

@ -6,29 +6,29 @@ OptionParser.parse do |parser|
parser.banner = "Welcome to eznetstat!"
parser.on "-v", "--version", "Show version" do
puts "0.1"
exit
puts "0.1"
exit
end
parser.on "-h", "--help", "Show help" do
puts parser
exit
puts parser
exit
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
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
ltnp_operator = NetstatRunner.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
exit
ltnp_operator = NetstatRunner.run_ltnp
ltnp_operator.say_hi
exit
end
end

@ -1,7 +1,7 @@
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
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

@ -1,7 +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
tokenized_record = record.split(" ")
tokenized_record = tokenized_record.reject { |s| s == "" }
tokenized_record
end
end

@ -0,0 +1,13 @@
name: eznet-cli
version: 0.0.1
authors:
- BraydonKains <kainsbraydon@gmail.com>
targets:
eznet-cli:
eznet: ./main.cr
crystal: 0.33.0
license: MIT

@ -5,15 +5,15 @@ describe LtnpOperator do
sut = LtnpOperator.new File.read("./spec/input/ltnp_example.txt")
describe "#get_port_record" do
existing_port = "8081"
nonsense_port = "1234"
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 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
it "should not find record when port doesn't exist" do
sut.get_port_record(nonsense_port).nil?.should be_true
end
end
end

@ -7,23 +7,23 @@ describe LtnpRecord do
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")
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")
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
sut_process.pid.empty?.should be_true
sut_process.p_name.empty?.should be_true
end
end

Loading…
Cancel
Save