When you want to crawl some websites and you want to hide yourself a little, here's a simple trick to change randomly your user-agent string.
There's a very convenient function in erlang called 'uniform' from the module 'random', you can use it by calling 'random:uniform(MaxValue)' where MaxValue is the high limit.
So if you want to generate a random value from 1 to 5 you can simple use, 'random:uniform(5)'...
Now that you know how to generate random values, here's some code that do just what's the title say:
EazyErl !
From simple code snippet to full blown module, find what you need and sometimes more :)
Wednesday, October 12, 2011
Thursday, July 29, 2010
erlang: Simple Debug macro
When testing things I need some quick way to debug "a la" printf() style. Old habits :)
Here's is the simple debug macro I use:
Then you need to define the "debug" atom to let your macro do what you want. The compile:file/2 handles options for this, the syntax is {d, debug}
Here's is the simple debug macro I use:
-ifdef(debug).
-define(DEBUG(Format, Args),
io:format("~s.~w: DEBUG: " ++ Format, [ ?MODULE, ?LINE | Args])).
-else.
-define(DEBUG(Format, Args), true).
-endif.
Write it down in a "debug.hrl" file, then you only need to add this line in any file header:-include("debug.hrl").
This simple macro gives you the module name and the line number. This saves me a lot of time.Then you need to define the "debug" atom to let your macro do what you want. The compile:file/2 handles options for this, the syntax is {d, debug}
Wednesday, February 24, 2010
Filtering lines efficiently
Whenever you are dealing with log lines or that you're program is filtering data you always have to handle 'escaping' efficiently.
While developing a log module using a gen_event, I needed to escape simple quotes.
Sometime thoses quotes were already escaped...
I've found this regexp to handle gracefully the case:
While developing a log module using a gen_event, I needed to escape simple quotes.
Sometime thoses quotes were already escaped...
I've found this regexp to handle gracefully the case:
re:replace( Bin, "(?<!\\\\)'", "\\\\'", [ global ] ).
Tuesday, February 23, 2010
Reading an openssl .priv.key file and extracting the key
Extracting the private key from a .priv.key file is simple.
The private key is encrypted using a AES-128 with your passphrase.
The initial vector is also stored in the file, you can extract it directly from the first line:
The private key is encrypted using a AES-128 with your passphrase.
The initial vector is also stored in the file, you can extract it directly from the first line:
get_salt( <<"Salted__", Salt:8/binary, Rest/binary>> ) ->
{Salt, Rest}.
Tuesday, September 22, 2009
erlang: Parsing binary data dynamically
Hi,
here's a quick tip for parsing binary data which format is unknown at compile time...
Let's say that you have a binary string and that later you receive its structure. Take for
example the code below:
Precisely, we want to slice the binary part into 4 parts described as '[4, 4, 2, 2]' where each element is the size.
Let's compile and run:
here's a quick tip for parsing binary data which format is unknown at compile time...
Let's say that you have a binary string and that later you receive its structure. Take for
example the code below:
-module(binm).
-export([test/0, test/2]).
test() ->
test(<<4,0,0,0,5,0,0,0,7,0,8,0,33,1>>, [ 4, 4, 2, 2]).
test(Bin, List) ->
{Final, End} = lists:foldl( fun(Len, {Res, Rest}) ->
case Rest of
<<M:Len/binary, NewRest/binary>> ->
{[ M | Res ], NewRest};
<<_:1/binary, NewRest/binary>> ->
{ Res, NewRest}
end
end, {[], Bin}, List),
{lists:reverse(Final), End}.
Precisely, we want to slice the binary part into 4 parts described as '[4, 4, 2, 2]' where each element is the size.
test() ->
test(<<4,0,0,0,5,0,0,0,7,0,8,0,33,1>>, [ 4, 4, 2, 2]).
Let's compile and run:
2> c(binm).
{ok,binm}
3> binm:test().
{[<<4,0,0,0>>,<<5,0,0,0>>,<<7,0>>,<<8,0>>],<<33,1>>}
4>
Isn't this nice ? :p
Subscribe to:
Posts (Atom)