aboutsummaryrefslogtreecommitdiff
path: root/shell
blob: 4c4321af0625c9b1b52d6703e4bc9f3bef094290 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
#
# Simple autossh and screen wrapper.
#

# Parameters
BASENAME="`basename $0`"
DEST="$1"
COMMAND="$2"

# Set window title
# http://stackoverflow.com/questions/899609/gnu-screen-run-script-that-sends-commands-to-the-screen-session-it-is-being-run
function window_title {
  if [ -n "$STY" ]; then
    screen -p $WINDOW -X title "$*"
  else
    xtitle "$*"
  fi
}

# Syntax check
if [ -z "$DEST" ]; then
  exit 1
fi

# Set default screen title
window_title $DEST

# Dispatcher
if [ "$BASENAME" == "shells" ]; then
  # Remote screen shell using autossh
  if [ "$COMMAND" == "root" ]; then
    autossh $DEST -t -- sudo screen -x
  else
    autossh $DEST -t -- screen -x $COMMAND
  fi
else
  if [ -z "$COMMAND" ] && screen -ls $DEST | grep -q "There is a screen on"; then
    # Local existing screen shell
    wscreen $DEST
  elif [ -z "$COMMAND" ] && [ "$DEST" == "root" ]; then
    # Local root shell
    window_title root
    sudo screen -x
  elif [ "$DEST" == "local" ]; then
    # Local screen shell
    window_title $COMMAND
    wscreen $COMMAND
  else
    # Remote shell using autossh
    autossh $DEST -t -- $COMMAND
  fi
fi

# Restore screen title
window_title terminal