Collection-Scripts/set_tmux_ssh.sh

55 lines
1.7 KiB
Bash
Executable File

# set_tmux_ssh.sh
#!/bin/bash
# Check if the "--remove" parameter is provided
if [[ $1 == "--remove" ]]; then
# Remove the wrapper script
rm -f ~/ssh_tmux_wrapper.sh
# Remove the SSH shell setting
sed -i '/export SHELL=~/ssh_tmux_wrapper.sh/d' ~/.bashrc
# Remove the ForceCommand setting
sudo sed -i '/ForceCommand ~/ssh_tmux_wrapper.sh/d' /etc/ssh/sshd_config
sudo service ssh restart
# Print removal success message
echo "tmux settings have been removed."
exit 0
fi
# Check if tmux is already installed
if ! command -v tmux &> /dev/null; then
# Install tmux
sudo apt-get update
sudo apt-get install -y tmux
fi
# Check if the wrapper script is already created
if [ ! -f ~/ssh_tmux_wrapper.sh ]; then
# Create a wrapper script for SSH shell
echo '#!/bin/bash' >> ~/ssh_tmux_wrapper.sh
echo 'if [[ -z $TMUX ]]; then' >> ~/ssh_tmux_wrapper.sh
echo ' tmux new-session -A -s ssh' >> ~/ssh_tmux_wrapper.sh
echo 'fi' >> ~/ssh_tmux_wrapper.sh
chmod +x ~/ssh_tmux_wrapper.sh
fi
# Check if the SSH shell is already set
if ! grep -q "export SHELL=~/ssh_tmux_wrapper.sh" ~/.bashrc; then
# Set the wrapper script as the default SSH shell
echo "export SHELL=~/ssh_tmux_wrapper.sh" >> ~/.bashrc
fi
# Check if the ForceCommand is already set
if ! grep -q "ForceCommand ~/ssh_tmux_wrapper.sh" /etc/ssh/sshd_config; then
# Update sshd_config to use the wrapper script as SSH shell
echo "ForceCommand ~/ssh_tmux_wrapper.sh" | sudo tee -a /etc/ssh/sshd_config > /dev/null
# Restart SSH service
sudo service ssh restart
fi
# Print success message
echo "tmux has been set as the default SSH shell. SSH session will terminate when tmux detaches."