Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

A sketch for an escript that functions similarly to the now defunct erl_call.

View ecall.escript
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#!/usr/bin/env escript
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
 
%% Copyright 2013 Ulf Wiger <ulf@wiger.net>
%% License: http://opensource.org/licenses/MIT
 
main(Args) ->
Options = check_args(Args),
start_dist(Options),
Result = rpc(Options),
io:fwrite("~w~n", [Result]),
halt(0).
 
 
check_args(["-a", Call|Args]) ->
[{call, parse_call(Call)}|check_args(Args)];
check_args(["-c", Cookie|Args]) ->
[{cookie, list_to_atom(Cookie)}|check_args(Args)];
check_args(["-h", HiddenName|Args]) ->
[{hidden_name, HiddenName}|check_args(Args)];
check_args(["-name", Name|Args]) ->
[{longnames, true},{node, Name}|check_args(Args)];
check_args(["-sname", Name|Args]) ->
[{longnames, false},{node, Name}|check_args(Args)];
check_args([_|Args]) ->
check_args(Args);
check_args([]) ->
[].
 
start_dist(Options) ->
UseLongNames = proplists:get_value(longnames, Options, true),
MyNodeName = case lists:keyfind(hidden_name, 1, Options) of
false ->
random_node_name();
{_, N} ->
N
end,
UseNodeName = handle_own_host(MyNodeName),
supervisor:delete_child(kernel_sup, net_sup),
supervisor:start_child(
kernel_sup, {net_sup, {erl_distribution, start_link,
[[UseNodeName, if UseLongNames -> longnames;
true -> shortnames
end]]},
permanent, infinity, supervisor, [erl_distribution]}),
case lists:keyfind(cookie, 1, Options) of
false -> ok;
{_, Cookie} ->
erlang:set_cookie(node(), Cookie)
end.
 
handle_own_host(Node) ->
case re:split(Node, "@", [{return, list}]) of
[Short, Host] ->
inet_db:set_hostname(Host),
list_to_atom(Short);
[_] ->
list_to_atom(Node)
end.
 
rpc(Options) ->
{_, Node} = lists:keyfind(node, 1, Options),
Longnames = proplists:get_value(longnames, Options, true),
FullNode = fullname(Node, Longnames),
{_, {M, F, A}} = lists:keyfind(call, 1, Options),
case net_kernel:hidden_connect_node(FullNode) of
true ->
rpc:call(FullNode, M, F, A);
false ->
{badrpc, nodedown}
end.
 
parse_call(Call) ->
case split(Call) of
[Mod] ->
{list_to_atom(Mod), start, []};
[Mod, Fun] ->
{list_to_atom(Mod), list_to_atom(Fun), []};
[Mod, Fun, Args] ->
{list_to_atom(Mod), list_to_atom(Fun), parse_args(Args)}
end.
 
random_node_name() ->
{N1,N2,N3} = erlang:now(),
random:seed(N3,N2,N1),
"ecall" ++ integer_to_list(random:uniform(10000)).
 
fullname(N, LongNames) ->
case re:run(N, "@", []) of
nomatch ->
if LongNames ->
Host = hostname(),
io:fwrite("Host = ~p~n", [Host]),
list_to_atom(N ++ "@" ++ Host);
true ->
Host = hd(re:split(hostname(),
"\\.", [{return, list}])),
io:fwrite("Host = ~p~n", [Host]),
list_to_atom(N ++ "@" ++ Host)
end;
{match,_} ->
list_to_atom(N)
end.
 
hostname() ->
case {inet_db:gethostname(),inet_db:res_option(domain)} of
{H,D} when is_list(D), is_list(H),
length(D)> 0, length(H)>0 ->
H ++ "." ++ D;
Other ->
error({hostname, Other})
end.
 
parse_args(Args) ->
case erl_scan:string(Args ++ ". ") of
{ok, Tokens, _} ->
case erl_parse:parse_term(Tokens) of
{ok, Term} ->
Term;
Other ->
io:fwrite("Parse error: ~p~n", [Other]),
erlang:halt(1)
end;
ScanErr ->
io:fwrite("Scan error: ~p~n", [ScanErr]),
halt(1)
end.
 
split(Str) ->
re:split(Str, "\\s", [{return, list}, {parts, 3}]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.