Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to connect to a remote SSH server with Elixir

I have been trying for a while to connect to a remote ssh server with elixir.

This is what I do in IEX:

[Macintosh] elixir/logglycious (master|…)> iex                                                                            15-07-20 0:11
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.0.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> :application.start(:crypto)
{:error, {:already_started, :crypto}}
iex(2)> :application.start(:public_key)
{:error, {:not_started, :asn1}}
iex(3)> :application.start(:asn1)
:ok
iex(4)> :application.start(:public_key)
:ok
iex(5)> :application.start(:ssl)
:ok
iex(6)> :application.start(:ssh)
:ok
iex(7)> :ssh.connect("my.server.co.uk", 22, [ { :user, 'my_username' } ])
{:error, {:options, {:socket_options, [:inet]}}}
iex(8)>

First of all, I must say this error message is not helping at all. I received great support from the community on Slack though. Someone suggested to start the inets application too. I did and retried to connect but I got the same error again.

What am I doing wrong ? More importantly, how can I find the solution to such a problem next time ?

[FIXED] There were multiple issues. First the server must be provided between single quotes. Then make sure that your public key does not require a passphrase. If it does, it can be passed as an option to the connect function. Also, it is not necessary to start all the applications I started. :ssh.startis the only one I needed.

like image 736
svarlet Avatar asked Jul 19 '15 23:07

svarlet


1 Answers

If we look at the documentation for :ssh.connect/3 we see that the host argument should be a string. Since it's documentation for an erlang function the string means a charlist. Single quotes creates charlist strings, double quotes create utf-8 encoded binary strings. Call the function like this instead: :ssh.connect('my.server.co.uk', 22, user: 'my_username').

like image 160
Eric Meadows-Jönsson Avatar answered Sep 19 '22 13:09

Eric Meadows-Jönsson