toolshed
toolshed copied to clipboard
When exiting ssh session on nerves device, Toolshed's exit function exits with an error code of 255
Describe the bug
When we are ssh into a nerves device, we use Toolshed's exit function to close our ssh connection. The problem is that unlike a normal ssh session closing, closing nerves ssh session looks like an error to our bash shell. E.g. normally when you do ssh remote.server
and exit afterwards, if you check echo $?
you get 0. But with ssh nerves.local
when you give command exit and check with echo $?
you get 255. This is the same error code you get if you failed to ssh into nerves device, thus making it impossible for a script to tell whether it failed to make connection with a nerves device or it made a connection and ran everything successfully.
To Reproduce
Overview: ssh into a nerves device and exit, next check the command's status with echo $?
. You can follow the commands as:
ssh nerves.local
exit
echo $?
$ 255
Expected behavior
Overview: When we exit from nerves ssh, output of echo $?
should be 0. If ssh nerves.local
command fails the output of
echo $?
should be 255.
ssh nerves.local
exit
echo $?
$ 0
SFTP with nerves works fine so if anyone wants to check that to confirm he can:
sftp nerves.local
ls
exit
$ echo $?
0
Thanks for the report. It looks like there are two issues:
- When anything fails with
ssh
, the exit code is 255 so you can't tell if it's a connection failure or a script failure. I don't know how to fix that other than to parse thessh
output. I.e., have your script print "Successfully connected" at the beginning and check for that. If you must have different exit codes, then that's an issue withssh
. -
exit
makesssh
return an exit code of 255
As a workaround, use ssh
's scripting interface for running commands:
$ ssh nerves.local '
IO.puts("hello")
IO.puts("world")
'
Warning: Permanently added 'nerves.local,192.168.7.98' (RSA) to the list of known hosts.
hello
world
:ok%
$ echo $?
0
Having said that, I agree that it would be really nice to fix exit
. I'm not sure how to signal to Erlang's :ssh
application to close the connection successfully from the IEx prompt. It seems like there ought to be a way, but I don't know without digging into Erlang group.erl
group leader implementation to see if there is some message that I can send to it. I know that there's not a simple :io_request
message that I can send, sadly. PRs would be really appreciated on this.
Thanks a lot @fhunleth for the response. The solution above works. I pasted this here just so that this remains as a pointer and in future someone might be able to fix it. Thing is that commands in bash heredocs shows the output from the device (which enhances the experience significantly, in my opinion) whereas, running using ssh nerves.local 'my commands 1; command 2;'
doesn't give that kind of an output.
I also found that using sftp nerves.local
works fine. I think I'll add this info in my original issue post as well.
@herozero777 Do you plan on submitting a PR to fix this issue?
I'd like to but I have no idea what is causing this issue or where to look for the solution. I checked the flags that can be passed to Process.exit/2
and they are :kill
and :normal
only. Using :kill
results in an error & using :normal
doesn't exit the ssh session (I'm guessing normal flag would mean don't touch the Process.group_leader())
def exit() do
Process.exit(Process.group_leader(), :kill)
<span class="pl-k">end</span>
Apart from this I'm out of ideas.