update script
This commit is contained in:
parent
70a16bbf8b
commit
43ecc226f7
1 changed files with 67 additions and 29 deletions
|
|
@ -1,53 +1,91 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Variables
|
||||
YUBIKEY_ID_DIR="$HOME/.config/yubikey"
|
||||
SSH_ID_RSA="$HOME/.ssh/id_rsa"
|
||||
|
||||
# Function to display an error message and exit
|
||||
error_exit() {
|
||||
echo "Error: $1" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Find YubiKeys
|
||||
yubikeys=$(ykman list)
|
||||
|
||||
# Check if any YubiKeys were found
|
||||
if [[ -z "$yubikeys" ]]; then
|
||||
echo "No YubiKeys found."
|
||||
exit 1
|
||||
error_exit "No YubiKeys found."
|
||||
fi
|
||||
|
||||
# Select a YubiKey using wofi
|
||||
selected=$(echo "$yubikeys" | wofi --dmenu -i)
|
||||
|
||||
# Check if the user made a selection
|
||||
if [[ -z "$selected" ]]; then
|
||||
echo "No selection made."
|
||||
exit 1
|
||||
error_exit "No YubiKey selected."
|
||||
fi
|
||||
|
||||
# Extract the YubiKey serial number
|
||||
YUBIKEY_SERIAL=$(echo "$selected" | awk '{print $NF}')
|
||||
if [[ -z "$YUBIKEY_SERIAL" ]]; then
|
||||
error_exit "Failed to extract YubiKey serial number."
|
||||
fi
|
||||
|
||||
if [ -L "$HOME/.ssh/id_rsa" ]; then
|
||||
rm "$HOME/.ssh/id_rsa"
|
||||
# Remove existing symbolic link if it exists
|
||||
if [ -L "$SSH_ID_RSA" ]; then
|
||||
rm -f "$SSH_ID_RSA"
|
||||
if [ $? -ne 0 ]; then
|
||||
error_exit "Failed to remove existing symbolic link."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Create the symbolic link
|
||||
ln -s "$HOME/.config/yubikey/id_$YUBIKEY_SERIAL" $HOME/.ssh/id_rsa
|
||||
chmod 600 "$HOME/.ssh/id_rsa"
|
||||
ln -s "$YUBIKEY_ID_DIR/id_$YUBIKEY_SERIAL" "$SSH_ID_RSA"
|
||||
if [ $? -ne 0 ]; then
|
||||
error_exit "Failed to create symbolic link."
|
||||
fi
|
||||
|
||||
# Set correct permissions
|
||||
chmod 600 "$SSH_ID_RSA"
|
||||
if [ $? -ne 0 ]; then
|
||||
error_exit "Failed to set permissions on symbolic link."
|
||||
fi
|
||||
|
||||
# Stop the current ssh-agent
|
||||
if [ -n "$SSH_AGENT_PID" ]; then
|
||||
kill "$SSH_AGENT_PID"
|
||||
ssh-agent -k
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Failed to stop existing ssh-agent. Continuing..."
|
||||
fi
|
||||
unset SSH_AGENT_PID SSH_AUTH_SOCK
|
||||
fi
|
||||
|
||||
# Start a new ssh-agent
|
||||
eval $(ssh-agent -s)
|
||||
if [ -z "$SSH_AGENT_PID" ]; then
|
||||
error_exit "Failed to start ssh-agent."
|
||||
fi
|
||||
|
||||
# Update tmux sessions
|
||||
# Update tmux sessions if tmux is running
|
||||
if tmux ls 2>/dev/null; then
|
||||
tmux ls 2>/dev/null | cut -d ':' -f1 | while read -r session_name; do
|
||||
tmux setenv -t "$session_name" SSH_AUTH_SOCK "$SSH_AUTH_SOCK"
|
||||
tmux setenv -t "$session_name" SSH_AGENT_PID "$SSH_AGENT_PID"
|
||||
tmux refresh-client -t "$session_name"
|
||||
echo "Updated tmux session: $session_name"
|
||||
done
|
||||
|
||||
# Check if the symbolic link was created successfully
|
||||
if [ -L "$HOME/.ssh/id_rsa" ]; then
|
||||
echo "Symbolic link created successfully at $target"
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Failed to update tmux session: $session_name. Continuing..."
|
||||
else
|
||||
echo "Failed to create symbolic link at $target"
|
||||
exit 1
|
||||
echo "Updated tmux session: $session_name"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
notify-send "SSH Key has been updated for $(echo $selected | awk '{split($0,a,"("); print a[1]}')"
|
||||
# Verify symbolic link creation
|
||||
if [ -L "$SSH_ID_RSA" ]; then
|
||||
echo "Symbolic link created successfully at $SSH_ID_RSA"
|
||||
else
|
||||
error_exit "Failed to create symbolic link at $SSH_ID_RSA"
|
||||
fi
|
||||
|
||||
# Extract the YubiKey name for the notification
|
||||
YUBIKEY_NAME=$(echo "$selected" | awk '{split($0,a,"("); print a[1]}')
|
||||
notify-send "SSH Key has been updated for $YUBIKEY_NAME"
|
||||
|
||||
exit 0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue