gazzer82

gazzer82

Unable to issue command/read data on remote host from Elixir using sshex framework

MacOS 12.5.1 (M1 Max)
Elixir 1.14.3 (compiled with Erlang/OTP 25)

I am trying to read some status data from a network switch within my Elixir application over ssh. I am attempting to do this using the SSHEx framework.

I am able to connect fine (returns ok, and I can see an open session on the switch), but when I issues a command I am unable to read any response, and it times out waiting.

To confirm I have started :ssh under Extra Application.

Here is the code I am using initially, I’m aware it’s not ideal at the moment, but it should at least be getting me some output.

    defmodule FibreTestUi.Ssh do
      defp connect do
        SSHEx.connect(
          ip: '192.168.1.1',
          user: 'admin',
          password: '!Password',
          modify_algorithms: [
            {:append, [{:kex, [:"diffie-hellman-group-exchange-sha1"]}]},
            {:append, [{:public_key, [:"ssh-rsa", :"ssh-dss"]}]}
          ]
        )
      end
    
      def list_port_status do
        {:ok, conn} = connect()
        SSHEx.cmd!(conn, 'show ssh', exec_timeout: 15000)
      end
    end

All I get back is:

    ** (RuntimeError) {:error, "Timeout. Did not receive data for 15000ms."}
        (sshex 2.2.1) lib/sshex.ex:100: SSHEx.cmd!/3
        iex:2: (file)

If I ssh into the switch manually, I am able to execute the command fine.

    user@comp ~ % ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 admin@192.168.1.1
    
    
    FS#show ssh
    Connection Version Encryption       Hmac         Compress   State           Username
             0     2.0 aes128-ctr       hmac-sha1    none       Session started admin
    FS#

If I pick another random device (ransom raspberry pi), and connect with a basic ‘echo hello’ I get a response fine.

    defmodule FibreTestUi.Ssh do
      defp connect do
        SSHEx.connect(
          ip: '10.11.xx.xx',
          user: 'pi',
          password: 'password',
          modify_algorithms: [
            {:append, [{:kex, [:"diffie-hellman-group-exchange-sha1"]}]},
            {:append, [{:public_key, [:"ssh-rsa", :"ssh-dss"]}]}
          ]
        )
      end
    
      def list_port_status do
        {:ok, conn} = connect()
        SSHEx.cmd!(conn, 'echo hello', exec_timeout: 5000)
      end
    end

"hello\n"

I have also tried doing this directly using :ssh with the following code:

      def send_command do
        {:ok, ssh_pid} =
          :ssh.connect('192.168.1.1', 22,
            user: 'admin',
            save_accepted_host: false,
            silently_accept_hosts: true,
            password: '!Password',
            modify_algorithms: [
              prepend: [kex: [:"diffie-hellman-group1-sha1", :"diffie-hellman-group-exchange-sha1"]],
              prepend: [public_key: [:"ssh-rsa", :"ssh-dss"]]
            ]
          )
    
        {:ok, chn} = :ssh_connection.session_channel(ssh_pid, 5000)
    
        _success = :ssh_connection.exec(ssh_pid, chn, 'show ssh', 5000)
    
        # for _ <- 0..1 do
        receive do
          {:ssh_cm, ^ssh_pid, value} -> IO.inspect(value)
        after
          5000 ->
            IO.puts(:stderr, "No message in 5 seconds")
        end
    
        :ok = :ssh.close(ssh_pid)
      end

The response I get is this, for every command, no matter what command I send.

    iex(20)> FibreTestUi.Ssh.send_command    
    {:data, 0, 0,
     <<2, 83, 67, 80, 58, 32, 83, 67, 80, 32, 83, 101, 114, 118, 101, 114, 32, 83,
       101, 114, 118, 105, 99, 101, 32, 105, 115, 32, 100, 105, 115, 97, 98, 108,
       101, 33, 10>>}

So it seems to be something specific to this device, but I can’t really work out what that would be. Anyone got any thoughts?

Most Liked

benstepp

benstepp

Not sure exactly how to solve the problem, but the binary reads:

"SCP: SCP Server Service is disable!\n"
gazzer82

gazzer82

Thanks all, I have decided to stop fighting this and just use the serial console port instead, seems the implementation on this switch is just a little “odd” and the serial port is, well a serial port, so a little easier to deal with . . .

Thanks

Gareth

Where Next?

Popular in Questions Top

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
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

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
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
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
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
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New

We're in Beta

About us Mission Statement