-
-
Save astromechza/b1af50261a3c1d6dc17f6c8b4f972886 to your computer and use it in GitHub Desktop.
Light weight MoSH using Tmux and BASH
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
| #! /bin/bash | |
| # usage: tssh <ssh_target> <tmux target> [<ssh args>] | |
| # requires ssh passwordless login to be setup and tmux to be installed on the remote machine | |
| set -eu | |
| # Count the number of args passed through, if there are not enough print the help. | |
| if [[ "$#" < "2" ]]; then | |
| echo "Not enough arguments where passed to the script." | |
| echo "Usage: $0 <ssh_target> <tmux target> [<ssh args>]" | |
| echo "<ssh_target> = the normal ssh target (user@host)" | |
| echo "<tmux target> = the tmux session to connect to or '' for the default" | |
| exit 1 | |
| fi | |
| # Pull the args from the command | |
| ssh_target=$1 | |
| tmux_target=$2 | |
| args=${*:3} | |
| ssh="ssh -o ConnectTimeout=4 -o ServerAliveInterval=4 -o ServerAliveCountMax=2 -o TCPKeepAlive=yes" | |
| # Based on Tmux target, run a different command. | |
| if [ ! -z $tmux_target ]; then | |
| cmd="tmux attach-session -t '$tmux_target' || tmux new-session -s '$tmux_target'" | |
| else | |
| # attach to the default target | |
| cmd="tmux attach-session || tmux new-session" | |
| fi | |
| # Extract the pingable host string from the ssh target | |
| ping_target=$(echo $ssh_target | cut -d"@" -f2) | |
| # Enter the connection loop | |
| echo "Beginning connection loop.." | |
| while true; do | |
| if $ssh "$ssh_target" $args -t "$cmd"; then | |
| break | |
| fi | |
| echo | |
| echo "Connection to \"$ping_target\" dropped at $(date)..." | |
| while ! ping -c 1 -t 3 "$ping_target" >/dev/null 2>&1; do | |
| sleep 10 | |
| done | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment