forked from kudelskisecurity/scannerl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathout_file_norm.erl
62 lines (52 loc) · 1.73 KB
/
out_file_norm.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
%% output module - output to file normalized
-module(out_file_norm).
-behavior(out_behavior).
-author("deadc0de6").
-export([init/2, clean/1, output/2]).
-export([get_description/0]).
-export([get_arguments/0]).
-define(ERR_ARG, "arg=[Output_file_path]").
-define(DESCRIPTION, "output to file normalized").
-define(ARGUMENTS, ["File path"]).
-record(opt, {path, fd}).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% API
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% this is the initialization interface for this module
%% returns {ok, Obj} or {error, Reason}
init(_Scaninfo, [Path]) ->
case file:open(Path, [write, delayed_write, {encoding, utf8}]) of
{ok, Fd} ->
Opts = #opt{path=Path, fd=Fd},
{ok, Opts};
{error, Reason} ->
{error, Reason}
end;
init(_Scaninfo, _) ->
{error, ?ERR_ARG}.
%% this is the cleaning interface for this module
%% returns ok or {error, Reason}
clean(Object) ->
file:close(Object#opt.fd).
get_description() ->
?DESCRIPTION.
get_arguments() ->
?ARGUMENTS.
%% this is the output interface
%% output'ing to file
%% returns ok or {error, Reason}
output(_Obj, []) ->
ok;
output(Obj, [H|T]) ->
output_one(Obj, H),
output(Obj, T).
output_one(Object, {_Mod, {A,B,C,D}, Port, {{ok,result},[Result]}}) ->
Out = io_lib:fwrite("~p.~p.~p.~p:~p,~p~n", [A,B,C,D,Port,Result]),
file:write(Object#opt.fd, Out);
output_one(Object, {_Mod, Hostname, Port, {{ok,result},[Result]}}) ->
Out = io_lib:fwrite("~s:~p,~p~n", [Hostname,Port,Result]),
file:write(Object#opt.fd, Out);
output_one(Object, {_, {A,B,C,D}, Port, Res}) ->
Out = io_lib:fwrite("ERROR: ~p.~p.~p.~p:~p,~p~n", [A,B,C,D,Port,Res]),
file:write(Object#opt.fd, Out),
ok.