Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh-agent not getting cleaned on unclean exit #23

Open
MagikEh opened this issue Feb 6, 2025 · 1 comment
Open

ssh-agent not getting cleaned on unclean exit #23

MagikEh opened this issue Feb 6, 2025 · 1 comment

Comments

@MagikEh
Copy link

MagikEh commented Feb 6, 2025

Issue:

When you run a command that invokes the sr3_ssh script, it sets up a ssh-agent but that agent's socket is not cleaned up if the script unexpectedly exits. This leads to an absolute metric truckload of $TMPDIR/ssh-* dirs being created on the host.

Steps to reproduce

ls -dl  $TMPDIR/ssh-*
> # nothing returned

sr3l 'echo blah'
> host1: blah
> host2: blah
> host3: blah

ls -dl  $TMPDIR/ssh-*
> # nothing returned because the agent was cleaned up properly


sr3l 'echo blah'
> host1: blah
^C  #<--- Force the run to stop before it completes.

ls -dl  $TMPDIR/ssh-*
drwx------ 2 user      user      26 Feb  6 19:58 /tmp/ssh-XXXXXX2j1w9S/

Suggested Fix:

Add a trap to the bash script for on-exit signals:

trap 'ssh-agent -k' EXIT
@MagikEh
Copy link
Author

MagikEh commented Feb 6, 2025

Attempted to run with

if [ -e "${ssh_user_config}" ] && { [ -z "${SSH_AUTH_SOCK}" ] || [ -z "${SSH_AGENT_PID}" ]; } && grep -q "ssh-add" "${ssh_user_config}"; then
    eval "$(ssh-agent -s)" > /dev/null
    trap 'ssh-agent -k' EXIT
    trap 'ssh-agent -k' ERR
fi

And commented out the Clean up ssh-agent code at the bottom and it worked for normal scenarios, but not the ctrl+c..

I wonder if it's because the ssh-agent is being executed within a child shell..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant