gazzer82

gazzer82

SSH Connection, unable to read any reply from host, others work fine

I am trying to read some data from a Cisco network switch (SG300-28SFP) over ssh.

Initially i couldn’t connect as the switch only support some pretty old ciphers e.t.c, but added those to the connection and i can connect. However i cannot read any replies from the device.

Initially i was trying sshex and sshkit, but am now using erlang :ssh directly to make sure it wasn’t a framework problem.

Here is my (pretty basic, and badly written), code.

  def send_command(command) do
    {:ok, ssh_pid} =
      :ssh.connect('192.168.1.254', 22,
        user: 'cisco',
        save_accepted_host: false,
        silently_accept_hosts: true,
        password: 'cisco',
        modify_algorithms: [
          prepend: [kex: [:"diffie-hellman-group1-sha1", :"diffie-hellman-group-exchange-sha1"]],
          prepend: [public_key: [:"ssh-rsa", :"ssh-dss"]]
        ]
        # ssh_msg_debug_fun: fn _, _, msg, _ -> IO.inspect(msg) end
        # user_dir: Path.expand("../../ssh", __DIR__)
      )

    IO.inspect(
      :ssh.connection_info(ssh_pid, [
        :client_version,
        :server_version,
        :user,
        :peer,
        :sockname,
        :options,
        :algorithums
      ])
    )

    {:ok, chn} = :ssh_connection.session_channel(ssh_pid, 5000)

    _success = :ssh_connection.exec(ssh_pid, chn, command, 5000)

    for _ <- 0..3 do
      receive do
        {:ssh_cm, ^ssh_pid, value} -> IO.inspect(value)
      end
    end

    :ok = :ssh.close(ssh_pid)
  end
end

If i run this, it hangs forever at the receive do loop, like this:

iex(1)> FibreTest.Cisco.send_command("up")
[
  client_version: {{2, 0}, 'SSH-2.0-Erlang/4.12.5'},
  server_version: {{2, 0}, 'SSH-2.0-OpenSSH_5.9p1.RL'},
  user: 'cisco',
  peer: {'192.168.1.254', {{192, 168, 1, 254}, 22}},
  sockname: {{192, 168, 1, 100}, 60691},
  options: [
    modify_algorithms: [
      prepend: [
        kex: [:"diffie-hellman-group1-sha1",
         :"diffie-hellman-group-exchange-sha1"],
        public_key: [],
        cipher: [client2server: [], server2client: []], 
        mac: [client2server: [], server2client: []],
        compression: [client2server: [], server2client: []]
      ],
      prepend: [
        kex: [],
        public_key: [:"ssh-rsa", :"ssh-dss"],
        cipher: [client2server: [], server2client: []],
        mac: [client2server: [], server2client: []],
        compression: [client2server: [], server2client: []]
      ]
    ],
    password: 'cisco',
    pref_public_key_algs: [:"ssh-rsa", :"ssh-dss", :"ecdsa-sha2-nistp384",
     :"ecdsa-sha2-nistp521", :"ecdsa-sha2-nistp256", :"ssh-ed25519",
     :"ssh-ed448", :"rsa-sha2-256", :"rsa-sha2-512"],
    preferred_algorithms: [
      kex: [:"diffie-hellman-group1-sha1",
       :"diffie-hellman-group-exchange-sha1", :"ecdh-sha2-nistp384",
       :"ecdh-sha2-nistp521", :"ecdh-sha2-nistp256",
       :"diffie-hellman-group-exchange-sha256",
       :"diffie-hellman-group16-sha512", :"diffie-hellman-group18-sha512",
       :"diffie-hellman-group14-sha256", :"curve25519-sha256",
       :"curve25519-sha256@libssh.org", :"curve448-sha512"],
      public_key: [:"ssh-rsa", :"ssh-dss", :"ecdsa-sha2-nistp384",
       :"ecdsa-sha2-nistp521", :"ecdsa-sha2-nistp256", :"ssh-ed25519",
       :"ssh-ed448", :"rsa-sha2-256", :"rsa-sha2-512"],
      cipher: [
        client2server: [:"chacha20-poly1305@openssh.com",
         :"aes256-gcm@openssh.com", :"aes256-ctr", :"aes192-ctr",
         :"aes128-gcm@openssh.com", :"aes128-ctr", :"aes256-cbc", :"aes192-cbc",
         :"aes128-cbc", :"3des-cbc"],
        server2client: [:"chacha20-poly1305@openssh.com",
         :"aes256-gcm@openssh.com", :"aes256-ctr", :"aes192-ctr",
         :"aes128-gcm@openssh.com", :"aes128-ctr", :"aes256-cbc", :"aes192-cbc",
         :"aes128-cbc", :"3des-cbc"]
      ],
      mac: [
        client2server: [:"hmac-sha2-256-etm@openssh.com",
         :"hmac-sha2-512-etm@openssh.com", :"hmac-sha2-256", :"hmac-sha2-512",
         :"hmac-sha1-etm@openssh.com", :"hmac-sha1"],
        server2client: [:"hmac-sha2-256-etm@openssh.com",
         :"hmac-sha2-512-etm@openssh.com", :"hmac-sha2-256", :"hmac-sha2-512",
         :"hmac-sha1-etm@openssh.com", :"hmac-sha1"]
      ],
      compression: [
        client2server: [:none, :"zlib@openssh.com", :zlib],
        server2client: [:none, :"zlib@openssh.com", :zlib]
      ]
    ],
    save_accepted_host: false,
    silently_accept_hosts: true,
    user: 'cisco'
  ]
]

If i ssh into the switch from the same machine manually, i get the expected login prompt.

Here’s my open_ssh config (which i also had to edit to enable me to connect)

# This is the ssh client system-wide configuration file.  See
# ssh_config(5) for more information.  This file provides defaults for
# users, and the values can be changed in per-user configuration files
# or on the command line.

# Configuration data is parsed as follows:
#  1. command line options
#  2. user-specific file
#  3. system-wide file
# Any configuration value is only changed the first time it is set.
# Thus, host-specific definitions should be at the beginning of the
# configuration file, and defaults at the end.

# This Include directive is not part of the default ssh_config shipped with
# OpenSSH. Options set in the included configuration files generally override
# those that follow.  The defaults only apply to options that have not been
# explicitly set.  Options that appear multiple times keep the first value set,
# unless they are a multivalue option such as IdentityFile.
Include /etc/ssh/ssh_config.d/*

# Site-wide defaults for some commonly used options.  For a comprehensive
# list of available options, their meanings and defaults, please see the
# ssh_config(5) man page.

# Host *
#   ForwardAgent no
#   ForwardX11 no
#   PasswordAuthentication yes
#   HostbasedAuthentication no
#   GSSAPIAuthentication no
#   GSSAPIDelegateCredentials no
#   BatchMode no
#   CheckHostIP yes
#   AddressFamily any
#   ConnectTimeout 0
#   StrictHostKeyChecking ask
#   IdentityFile ~/.ssh/id_rsa
#   IdentityFile ~/.ssh/id_dsa
#   IdentityFile ~/.ssh/id_ecdsa
#   IdentityFile ~/.ssh/id_ed25519
#   Port 22
Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc
MACs hmac-md5,hmac-sha1,hmac-sha2-256,umac-64@openssh.com
#   EscapeChar ~
#   Tunnel no
#   TunnelDevice any:any
#   PermitLocalCommand no
#   VisualHostKey no
#   ProxyCommand ssh -q -W %h:%p gateway.example.com
#   RekeyLimit 1G 1h
#   UserKnownHostsFile ~/.ssh/known_hosts.d/%k
Host *
    SendEnv LANG LC_*
HostkeyAlgorithms ssh-dss,ssh-rsa
KexAlgorithms +diffie-hellman-group1-sha1,diffie-hellman-group14-sha1

I have various other linux/debian servers as well as some Pi’s that i can issue commands on from elixir/erlang fine.

So, a bit stumped here and wondering if anyone had any suggestions before i resort to the serial port :frowning:

Thanks!

Most Liked

trisolaran

trisolaran

Ok, I know this probably doesn’t help you much, but I would rewrite the code into:

 :success = :ssh_connection.exec(ssh_pid, chn, command, 5000)

To make sure it stops there if that step doesn’t succeed.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
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 ...
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
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
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
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