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:

https://github.com/rubencaro/sshex/blob/9758aeba36d94efbac061a03e57d673fc031591f/lib/sshex.ex#L226-L250

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:

https://github.com/rubencaro/sshex/blob/master/lib/sshex/configurable_client_keys.ex#L10-L26

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 — OTP 29.0.2 (ssh 6.0.1)

The parameters that can be sent to the client/daemon are also clearly documented, take a look through them: ssh — OTP 29.0.2 (ssh 6.0.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.

Last Post!

spinlock99

spinlock99

Thanks for this branch. I’ve got a pretty stock bootleg and sshkit but now that I’ve used your branch of ssh_client_key_api I’m able to authenticate with ed25519!

It’s too bad that Labzero isn’t maintaining bootleg and ssh_client_key_api because they are good solutions for exactly what I’m trying to do.

Where Next?

Popular in Questions Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement