Customizing Tmux Behavior and SSH Banner Display

Tmux is a versatile terminal multiplexer that allows you to manage multiple terminal sessions within a single window. Additionally, you can customize Tmux behavior to suit your preferences and automate tasks. This article presents a collection of Bash scripts designed to customize Tmux behavior and display an SSH banner when starting Tmux. Let’s explore each script’s purpose and functionality.

Script 1: Show SSH Banner on Start Tmux

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
if [[ -z "$TMUX" ]]; then
    tmux has-session &> /dev/null
    if [ $? -eq 1 ]; then
        shopt -q login_shell && exec tmux new
        exit
    else
        shopt -q login_shell && exec tmux attach
        exit
    fi
else
    TMUX_WINDOW=$(tmux display-message -p '#{session_windows}')
    if [ $TMUX_WINDOW == "1" ]; then
        shopt -q login_shell && run-parts /etc/update-motd.d/
    fi
fi

This script is designed to display an SSH banner when starting Tmux. It checks whether the TMUX environment variable is empty, which indicates that Tmux is not currently running. If Tmux is not running and the current shell is a login shell, it starts a new Tmux session. If Tmux is already running, it attaches to the existing session. If there’s only one window in the Tmux session, the script runs the /etc/update-motd.d/ script to show a message of the day for SSH sessions.

Script 2: Show Only First Window on Tmux Session

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
if [[ -z "$TMUX" ]]; then
    tmux has-session &> /dev/null
    if [ $? -eq 1 ]; then
        exec tmux new
        exit
    else
        exec tmux attach
        exit
    fi
else
    TMUX_WINDOW=$(tmux display-message -p '#{session_windows}')
    if [ $TMUX_WINDOW == "1" ]; then
        run-parts /etc/update-motd.d/
    fi
fi

Similar to the first script, this one focuses on Tmux behavior. It checks whether Tmux is running. If it’s not, a new Tmux session is started. If Tmux is already running, the script attaches to the existing session. If the Tmux session contains only one window, it executes the /etc/update-motd.d/ script.

Script 3: Show Every Time Start New Tmux Session

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
if [[ -z "$TMUX" ]]; then
    tmux has-session &>/dev/null
    if [ $? -eq 1 ]; then
        exec tmux new
        exit
    else
        exec tmux attach
        exit
    fi
else
    run-parts /etc/update-motd.d/ # Show every time new Tmux session
fi

This script focuses on executing the /etc/update-motd.d/ script every time a new Tmux session is started, regardless of the number of windows. If Tmux is not running, a new session is started. If Tmux is already running, the script attaches to the existing session and executes the update script.

Script 4: Pause Before Executing Tmux

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
if [[ -z "$TMUX" ]]; then
    read -n 1 -s -r -p "Press any key or wait 10 seconds to continue..." -t 10
    tmux has-session &>/dev/null
    if [ $? -eq 1 ]; then
        exec tmux new
        exit
    else
        exec tmux attach
        exit
    fi
fi

This script adds an interactive feature by pausing before executing Tmux. It prompts the user to press any key or wait for 10 seconds. After the pause, the script checks if Tmux is running. If not, a new Tmux session is started. If Tmux is already running, the script attaches to the existing session.

0%