Created
October 3, 2017 17:11
-
-
Save joh6nn/07c38b761c6a05f36bdb83b9c9aaaeca to your computer and use it in GitHub Desktop.
When logging in via ssh, automatically start a screen session if one is not running. Otherwise, display a list of available screen sessions, and automatically reconnect to the first available session after a timeout
This file contains 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 | |
# when logging in via ssh, automatically start a screen session if one is not | |
# running. otherwise, display a list of available screen sessions, and | |
# automatically reconnect to the first available session after a timeout | |
# Copyright (C) 2011 joh6nn@gmail.com | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
#CONFIG: | |
screen_opts="" | |
timeout="${AUTO_SCREEN_TIMEOUT:-10}" | |
# If not running interactively, don't do anything | |
[ -z "$PS1" ] && return | |
#check if we're in a ssh connection | |
if [ "$SSH_CONNECTION" ]; then | |
#check if screen is currently running | |
if [ -z "$STY" ]; then | |
#get list of screen sessions | |
screen_list=$(screen -ls | head -n -2 -) | |
#check to make sure list of screen sessions isn't empty | |
if [ `echo -e "$screen_list" | egrep -c "[0-9]+\.([a-zA-Z0-9\-]+)?\.[a-zA-Z]*"` -gt 0 ]; then | |
#display header from session list (but not the list) | |
echo -e "\n$screen_list" | head -n 2 - | |
#trim footer from list of screen sessions | |
screen_list=$(echo -e "$screen_list" | tail -n +2) | |
#display session list with line numbers (it's a menu) | |
echo -e "\n$screen_list" | awk 'NR>1 { LINE+=1; printf "\t%d%s\n",LINE,$0; }' | |
echo | |
# prompt, timeout a/f 10s, read only 1 char, default to most recent session | |
# bash builtin read stores user input in $REPLY by default | |
read -n 1 -t ${timeout} -p "Pick a # to reconnect to, or N to start new session: " | |
if [[ "n" = "$REPLY" ]]; then | |
#start a new session, so don't need any options | |
#intentional no-op | |
screen_opts=$screen_opts | |
elif [ -z "$REPLY" ]; then | |
#$REPLY is empty, so either we timed out or user hit enter | |
#default action is to force-reconnect to the first available screen session | |
screen_opts="-D -r" | |
else | |
#get the Nth entry from the list of screen sessions, and force-reconnect to that session | |
REPLY=$(echo -e "$screen_list" | awk -v REPLY=$REPLY 'NR==REPLY { print $1}' ) | |
screen_opts="-D -r $REPLY" | |
fi | |
fi | |
exec screen $screen_opts | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment