mody5

mody5

How to connect via :ssh module using an id_ed25519 private key string

I spent more than a day trying to figure out how to connect via ssh using a private key but without a result… all languages some to have an example when it comes to ssh except erlang/elixir which I don’t know why it’s that hard.

The only example I can find is using username/password, but I want to ssh to another machine using username and private ssh key (id_ed25519),

Any idea?

Most Liked

al2o3cr

al2o3cr

A good starting point IMO is the example in the :ssh user guide.

That shows the general shape of things (connect/session_channel/exec) but doesn’t really use the messages, or answer your original question.

For the “using the messages” part, take a look at how SSHEx does it - the whole file is worth reading, but this is where the actual {:ssh_cm, ...} messages get handled:

For the second half, there are two behaviours that :ssh depends on but the one we care about is :ssh_client_key_api. That’s implemented by two modules supplied with :ssh, :ssh_file and :ssh_agent which use either files or the SSH agent. :ssh_file is the default, but you can pass an alternative implementation via the :key_cb option to :ssh.connect.

HOWEVER

:ssh_client_key_api has more moving parts than just “here is a key”, it also deals with known hosts. SSHEx includes an implementation you could use / model after:

D4no0

D4no0

If you are using OTP, it’s always worth checking out the official documentation. Here is how to configure custom keypairs for servers and the next paragraph is for clients: ssh — ssh v5.2.1

The parameters that can be sent to the client/daemon are also clearly documented, take a look through them: ssh — ssh v5.2.1

mody5

mody5

Ok I believe this is so easy for you so please just add that one line or 2 missing lines where I have put a placeholder for completion (my private key is located in /home/me/.ssh/id_ed25519)

ssh_options = [
      {:user, user},
      {:silently_accept_hosts, true},
      {:user_interaction, false},
      # ====== please just fill in here those easy missing lines ======
    ]

{:ok, conn} <- :ssh.connect(ip, port, ssh_options, 5000)

BTW I am really newbie to OTP, but I am sure you an experienced on these stuff.

D4no0

D4no0

You can PM me and I’ll hand you the consulting prices, then you won’t have to wait anymore :slight_smile: .

mudasobwa

mudasobwa

Creator of Cure

For the record, I re-read the comment you are referring to three times and hereby I admit it does not have wording “it is easy to do.”

It suggests reading docs. Which is the great advice, btw. You might also read something about SSH in a nutshell, because your attempt to pass the username alongside with the file hints you are not quite familiar with it.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement