I’m working on some code that logs into routers and gathers information from them using SSH. I’m using the SSHEx module from hex.pm, which is a wrapper of Erlang ssh with some helper functions.
In the function that does the heavy lifting, I occasionally get an error that I can’t figure out. Here is the function:
def connect_and_run({hostname, command, receiver_pid}) do
# :ssh_dbg.start()
# :ssh_dbg.on()
# :ssh.start()
case SSHEx.connect(
ip: to_charlist(hostname),
user: @creds[:username],
password: @creds[:password],
auth_methods: ~c"password",
connect_timeout: 7000,
negotiation_timeout: 7000,
modify_algorithms: [
{:append, [{:public_key, [:"ssh-rsa"]}, {:kex, [:"diffie-hellman-group1-sha1"]}]}
]
) do
{:ok, conn} ->
sec = :rand.uniform(300)
:timer.sleep(sec)
output = run_command(conn, hostname, command)
output = "*** #{hostname} ***\n" <> output
send(receiver_pid, output)
{:error, reason} ->
send(receiver_pid, "*** Error connecting to #{hostname}: #{reason} ***\n")
end
end
If the command is successful, it sends the output from the command to a receiver process that is gathering all the responses. If an error happens, it sends an error to the receiver with some details.
Once in a while, I get this error:
20:58:09.257 [error] Process #PID<0.394.0> raised an exception
** (Protocol.UndefinedError) protocol String.Chars not implemented for {:options, []} of type Tuple. This protocol is implemented for the following type(s): Atom, BitString, Date, DateTime, Float, Integer, List, NaiveDateTime, Time, URI, Version, Version.Requirement
(elixir 1.14.0) lib/string/chars.ex:3: String.Chars.impl_for!/1
(elixir 1.14.0) lib/string/chars.ex:22: String.Chars.to_string/1
(sshtest 0.1.0) lib/multissh.ex:95: MultiSSH.connect_and_run/1
I’m stumped because neither my code nor the SSHEx module have a tuple that looks like {:options, }. I have no idea where that is coming from or what the real problem might be. It says the problem is on line 95 in my code, which is:
end(receiver_pid, "*** Error connecting to #{hostname}: #{reason} ***\n")
How in the world do I troubleshoot this?
Thanks!