gazzer82

gazzer82

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!

Showing Posts 1 to 5

trisolaran

trisolaran

Have you inspected what’s actually being returned here?

gazzer82

gazzer82 OP

Deleted

gazzer82

gazzer82 OP

Sorry seem to have replied twice, and can’t see a way to delete the first reply . . .

Yes it’s a :success atom.

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.

gazzer82

gazzer82 OP

Yes entirely valid, and this is not how I plan to actually do this if I can get it to work :+1:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews