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

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New

Other popular topics Top

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
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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