Last active
April 17, 2023 17:56
-
-
Save pipelinedave/b8f3d30ffcb9f54fb7f6684c110b5ce9 to your computer and use it in GitHub Desktop.
kde-konsole-ssh-profile-switcher-bash-script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# KDE Konsole SSH Profile Switcher | |
# A shell function for KDE Konsole terminal users to automatically switch the | |
# terminal profile based on the SSH hostname, and restore the default profile | |
# upon disconnection. Works with additional SSH options. | |
# Compatible with Bash and Zsh. | |
# Make sure to create corresponding Konsole profiles for each SSH hostname | |
# before using this function. | |
# For example, if you're connecting to "example.com", create a profile named | |
# "example.com" in Konsole with your desired settings. | |
# Add this function to your shell configuration file (e.g., ~/.bashrc for Bash | |
# or ~/.zshrc for Zsh), then restart your terminal or source the configuration | |
# file (e.g., `source ~/.bashrc` for Bash or `source ~/.zshrc` for Zsh) to | |
# apply the changes. | |
# To add this function to your .bashrc using a single command, run: | |
# curl -Ls "https://gist.github.com/pipelinedave/b8f3d30ffcb9f54fb7f6684c110b5ce9/raw" >> ~/.bashrc && source ~/.bashrc | |
# Function to change the Konsole profile | |
change_konsole_profile() { | |
local profile_name="$1" | |
if [ -z "$profile_name" ]; then | |
echo "Error: No profile name provided." | |
exit 1 | |
fi | |
if [ -z "$KONSOLE_DBUS_SESSION" ]; then | |
echo "Error: KONSOLE_DBUS_SESSION environment variable not found." | |
exit 1 | |
fi | |
qdbus org.kde.konsole "$KONSOLE_DBUS_SESSION" org.kde.konsole.Session.setProfile "$profile_name" | |
} | |
# Function to extract the hostname and other options from SSH command | |
parse_ssh_args() { | |
local ARGS=("$@") | |
local HOSTNAME="" | |
local SSH_OPTIONS=() | |
for arg in "${ARGS[@]}"; do | |
if [[ "$arg" != -* ]]; then | |
HOSTNAME="$arg" | |
break | |
else | |
SSH_OPTIONS+=("$arg") | |
fi | |
done | |
echo "$HOSTNAME" | |
return | |
} | |
# Function to wrap around the ssh command | |
ssh() { | |
local ARGS=("$@") | |
local HOSTNAME=$(parse_ssh_args "${ARGS[@]}") | |
local PROFILE_NAME="Default" | |
if [[ ! -z "$HOSTNAME" ]]; then | |
PROFILE_NAME="$HOSTNAME" | |
change_konsole_profile "$PROFILE_NAME" | |
fi | |
command ssh "${ARGS[@]}" | |
if [[ ! -z "$HOSTNAME" ]]; then | |
change_konsole_profile "Default" # Replace "Default" with your default profile name if different | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment