MetaStack
MetaStack

Reputation: 3696

Elixir / Erlang run and communicate with external executable

My elixir script seems to choke on the data that is returned by an external executable.

this signer program takes two arguments and returns a digest string.

> # Usage: signer [datahex] [privatehex]
> ./signer  64bd...  b3bd...
> 3a9c2e0220540ea2d2edbe18...

I can run it manually and it works great.

But when I run it with this elixir/erlang code:

"signer test string" |> String.to_char_list |> :os.cmd

it gives an error:

'2017/05/11 07:16:44 encoding/hex: invalid byte: U+0074 \'t\'\n'

So how do I get :os.cmd not to choke on the returned string? do I need to convert it somehow?

EDIT:

I thought it might be important to tell you I'm able to get it to work fine with other programs:

"notepad test.txt" |> String.to_char_list |> :os.cmd

results in notepad opening an existing file in the current working directory called test.txt. So I don't think the problem is with the call itself, but what do I know?

Upvotes: 1

Views: 264

Answers (1)

Dogbert
Dogbert

Reputation: 222040

The program accepts hexadecimal strings while you're passing "test", causing its decoder to fail at the first character, t, which is not a valid hex char. You need to pass valid hex strings.

You can use Base.encode16 if you meant to pass the string test literally:

"signer #{Base.encode16("test")} #{Base.encode16("string")}" |> String.to_char_list |> :os.cmd

I'd also recommend using System.cmd here for shorter and cleaner code as well as lesser chances of accidentally creating Command Injection vulnerabilities:

System.cmd("signer", [Base.encode16("test"), Base.encode16("string")])

Upvotes: 3

Related Questions