Screen: Difference between revisions

From Halfface
Jump to navigation Jump to search
(Created page with '=external links= http://www.gnu.org/software/screen/manual/screen.html =functions= <nowiki><pre> # Setup a screen session for an environment. screen_site(){ local SITE SAVEIFS …')
 
No edit summary
Line 2: Line 2:
http://www.gnu.org/software/screen/manual/screen.html
http://www.gnu.org/software/screen/manual/screen.html
=functions=
=functions=
<nowiki><pre>
<pre><nowiki>
# Setup a screen session for an environment.
# Setup a screen session for an environment.
screen_site(){
screen_site(){
Line 117: Line 117:
}
}


</pre></nowiki>
</nowiki></pre>

Revision as of 07:46, 14 November 2011

external links

http://www.gnu.org/software/screen/manual/screen.html

functions

# Setup a screen session for an environment.
screen_site(){
  local SITE SAVEIFS NUMBER MACHINE TITLE
  SITE=${1}
  SCREENSESSION=$(screen -ls | grep .${SITE})
  if [ "${SCREENSESSION}" ]
  then
    echo "Screen ${SITE} exists. Conncting to:"
    echo "${SCREENSESSION}"
    sleep 2
    screen -rx ${SITE}
    return
  else
    SAVEIFS=${IFS}
    NUMBER="0"
    IFS=$(echo -en "\n\b")
    screen -S ${SITE} -d -m
    for ROW in $(echo -e "${!SITE}")
    do
       MACHINE=$(echo ${ROW} | awk '{print $1}')
       TITLE=$(echo ${ROW} | awk '{print $2}')
       screen_ssh ${SITE} ${NUMBER} ${MACHINE} ${TITLE}
       NUMBER=$((NUMBER +1))
    done
    IFS=${SAVEIFS}
    screen -r ${SITE}
   fi
}

# Create new window and give it a title and connect to machine.
screen_ssh(){
    local SITE NUMBER MACHINE TITLE
    SITE="${1}"
    NUMBER="${2}"
    MACHINE="${3}"
    TITLE="${4}"
    if [ "${NUMBER}" -ne 0 ]
    then
      screen -S ${SITE} -X screen ${NUMBER}
    fi
    screen -S ${SITE} -X -p ${NUMBER} select ${NUMBER}
    screen -S ${SITE} -X -p ${NUMBER} title "${SITE}_${TITLE}"
    screen -S ${SITE} -X -p ${NUMBER} exec ssh -Y "${MACHINE}"
    screen -S ${SITE} -X -p ${NUMBER} wall "Connected to ${MACHINE} ${TITLE}"
}

# Group of machines machines needed by screen_site.
T1="10.1.93.10 RAPPO
10.1.21.10 NAPS1
10.1.21.11 NAPS2
10.1.21.12 NAPS3
10.1.21.13 NAPS4
10.1.21.14 NAPS5
10.1.21.15 NAPS6
10.1.21.16 NAPS6
10.1.21.40 NAPSGH1
10.1.21.50 NAPS-HELP
10.1.21.150 PLS1
10.1.21.151 PLS2"

# Run a command in a group of windows.
screen_cluster(){
  if [ $# -lt 3 ]
  then
    echo "You wrote: ${FUNCNAME} ${@}"
    echo "${FUNCNAME} SESSIONNAME 'command' SCREENS"
    SESSIONNAMES=$(screen -ls | grep -E '[[:blank:]][0-9]{2,}' | awk '{print $1}' | sed 's/^[0-9.]*//g')
    echo "SESSIONNAME available are:"
    echo "${SESSIONNAMES}"
  else
    SESSIONNAME=${1}
    COMMAND=${2}
    shift 2
    SCREENS="${@}"
    for SCREEN in ${SCREENS}
    do
      screen -S ${SESSIONNAME} -p ${SCREEN} -X stuff "${COMMAND}
"
      if [ $? -ne 0 ]
      then
        screen_cluster
        return 1
      fi
    done
  fi
}

# Split screen in regions with wanted windows.
screen_split(){
  if [ $# -lt 2 ]
  then
    echo "Splits screen in regions and show chosen windows."
    echo "${FUNCNAME} SESSIONNAME SCREENS"
  else
    SCREENSESSION="${1}"
    shift
    WINDOWSTART="${1}"
    NUMBERSCREEN="${#}"
    shift
    WINDOWSOPEN="${@}"
    screen -S ${SCREENSESSION} -p ${WINDOWSTART} -X select ${WINDOWSTART}
    screen -S ${SCREENSESSION} -p ${WINDOWSTART} -X only
    sleep 0.2
    for WINDOWOPEN in ${WINDOWSOPEN}
    do
      sleep 0.2
      screen -S ${SCREENSESSION} -p ${WINDOWSTART} -X split
      screen -S ${SCREENSESSION} -p ${WINDOWSTART} -X focus
      screen -S ${SCREENSESSION} -p ${WINDOWOPEN} -X select ${WINDOWOPEN}
    done
    screen -S ${SCREENSESSION} -p ${WINDOWSTART} -X wall "Opened ${NUMBERSCREEN} screen windows"
  fi
}