The “Wizard tool” tcpdump

Posted by marian on July 22, 2009

Shortly speaking TCPDUMP is an excellent tool for dumping the network traffic for latter analysis.
Personally I like:

  1. tcpdump -w sample.cap -s 0

This dumps all the network traffic to the file sample.cap while keeping the whole packets intact.
One should notice that using the “-s” option you can specify how many bytes from the start of packet will be retained.
The resulting file can be further interpreted and important conclusion can be drawn.

Get information about the computer’s CPU on a *nix using Erlang

Posted by marian on July 22, 2009

Problem statement: Get information about the computer’s CPU on a *nix (both Linux && Unix) machine.
Use case scenario:

  1. Compile the module
    1. 1> c(cpuinfo).
    2. {ok,last}
  2. Use it:
    1. 2> cpuinfo:details().

    [["processor","0"],
    ["vendor_id","GenuineIntel"],
    ["cpu family","6"],
    ["model","23"],
    ["model name","Intel(R) Core(TM)2 Duo CPU T8100@ 2.10GHz"],
    ["stepping","6"],
    ["cpu MHz","800.000"],
    ["cache size","3072 KB"],
    ["physical id","0"],
    ["siblings","2"],
    ["core id","0"],
    ["cpu cores","2"],
    ["apicid","0"],
    ["initial apicid","0"],
    ["fpu","yes"],
    ["fpu_exception","yes"],
    ["cpuid level","10"],
    ["wp","yes"],
    ["flags",
    "fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 lahf_lm ida tpr_shadow vnmi flexpriority"],
    ["bogomips","4189.71"],
    ["clflush size","64"],
    ["cache_alignment","64"],
    ["address sizes","36 bits physical, 48 bits virtual"],
    ["power management","\n"],
    ["\n"],
    ["processor","1"],
    ["vendor_id",[...]],
    [[...]|…],
    [...]|…]

  3. Source code:
    1. -module(cpuinfo).
    2. -export([details/0,removeUnwantedCharsFromList/2,frontSpace/2]).
    3.  
    4. frontSpace([H,HH|T],R) ->
    5.  if
    6.   (H=:=32 andalso HH=:=32) -> frontSpace(T,R); %space
    7.   (H=:=32 andalso HH=/=32) -> frontSpace([HH|T],[H]++R); %space
    8.   H=:=9 -> frontSpace([HH|T],R); %\t
    9.   H=:=10 -> frontSpace([HH|T],R); %\n
    10.   true   -> frontSpace([HH|T],[H]++R)
    11.  end;
    12. frontSpace(H,R) ->
    13.  if
    14.   H=:=[] -> lists:reverse(R);
    15.   (H=/=9 andalso H=/=10)-> lists:reverse(H++R)
    16.  end.
    17. removeChar([H|Word])->
    18.  if
    19.   H=:=32 -> frontSpace(Word,[]);
    20.   true -> frontSpace([H|Word],[])
    21.  end.  
    22.  
    23. removeUnwantedCharsFromList([Word|Words],Result)->
    24.  L1 = removeChar(Word),
    25.  L2 = removeChar(lists:reverse(L1)),
    26.  R = lists:reverse(L2),
    27.  removeUnwantedCharsFromList(Words, [R] ++ Result);
    28. removeUnwantedCharsFromList([],Result)->lists:reverse(Result).
    29. extractDataFromLine(Line) ->
    30.  {ok,Values} = regexp:split(Line,"[:]+")
    31.  %,L1 = frontSpace(Values,[])
    32.  ,[removeUnwantedCharsFromList(Values,[])].
    33. extractData(File,List)->
    34.  Line = io:get_line(File,''),
    35.  if
    36.   Line == eof ->  lists:reverse(tl(List));
    37.   true -> extractData(File,
    38.     extractDataFromLine(Line) ++ List)
    39.  end.
    40. details() ->
    41.  {ok, File} = file:open(paths:cpuinfo(),read),
    42.       extractData(File,[]).

Substitution cipher

Posted by marian on July 19, 2009

Use case scenario:

  1. Compile the module
  2. 1> c(cipher).
    1. {ok,last}
  3. Use it:
  4. 2> cipher:substitutionCipher(“dan”,[100,97,110],[97,100,110]).
    1. "adn"
  5. Source code:
  6. -module(cipher).
    1. -export([substitutionCipher/3]).
    2.  
    3. makeTranslation([],[],L)->L;
    4. makeTranslation([X|Xs],[Y|Ys],L)->makeTranslation(Xs,Ys,[{X,Y}|L]).
    5. makeTranslation(OriginalAlphabet,CryptedAlphabet)->makeTranslation(OriginalAlphabet,CryptedAlphabet,[]).
    6.  
    7. getTranslation(E,[{X,Y}|Tail])->
    8.  case E==X of
    9.    true -> Y;
    10.    false-> getTranslation(E,Tail)
    11.  end;
    12. getTranslation(_,[])->false.
    13.  
    14. substitute([],_,Substitution)->lists:reverse(Substitution);
    15. substitute([H|RestOfMessage],Translation,Substitution)->
    16. substitute(RestOfMessage,Translation,[getTranslation(H,Translation)]++Substitution).
    17.  
    18. substitutionCipher(Message,OriginalAlphabet,CryptedAlphabet)-> Translation = makeTranslation(OriginalAlphabet,CryptedAlphabet), substitute(Message,Translation,[]).

Last element in a list : 99 erlang problems

Posted by marian on July 18, 2009

Problem statement: Get the last element in a list.
Use case scenario:

  1. Compile the module
  2. 1> c(last).
    1. {ok,last}
  3. Use it:
  4. 2> last:last([1,2,3,4]).
    1. 4

Solution nr. 1:

-module(last).
  1. -export([last/1]).
  2.  
  3. last([LastOne])-> LastOne;
  4. last([_|Tail])-> last(Tail).

Solution nr. 2:

-module(last).
  1. -export([last/1]).
  2.  
  3. last(List)->hd(lists:reverse(List)).