Quantcast
Channel: linuxadmin: Expanding Linux SysAdmin knowledge
Viewing all articles
Browse latest Browse all 18193

Just want to share my Gnu Screen setup. It may be useful for other admins

$
0
0

I love system stats. But i also love a sense of organization considering how many systems I stay sshd'd into.

Here's my screensetup:

.screenrc

 altscreen on shell "/bin/bash" TERM=xterm #backtick 1 60 60 $HOME/bin/screen/get_uptime #backtick 2 60 60 $HOME/bin/screen/get_cputemp Core0 backtick 2 60 60 $HOME/bin/screen/get_freemem #backtick 3 60 60 $HOME/bin/screen/get_freemem backtick 3 60 60 $HOME/bin/screen/get_freeswap #backtick 4 60 60 $HOME/bin/screen/get_freeswap backtick 4 60 60 $HOME/bin/screen/get_uptime backtick 5 60 60 $HOME/bin/screen/get_sessionname #backtick 5 60 60 $HOME/bin/screen/get_volume PCM backtick 6 60 60 $HOME/bin/screen/get_gmail #backtick 7 60 60 $HOME/bin/screen/get_updates bind m exec $HOME/bin/screen/screen_theme.sh caption always "%{= KW}%-w%{= Yk}%n %t%{-}%+w %-=" hardstatus alwayslastline "%{= kW} %-= %{= kY}Session: %u%{= kW}%5` | %{= kY}Host:%{= kW} %H | %{= kY} Uptime:%{= kW} %4` | %{= kY} MEM:%{= kW} %2`MB |%{= kY} SW: %{= kW}%3`MB | %{= kY} DATE:%{= kW} %m/%d/%y %C %A" # Turn welcome message off startup_message off # disable vbell (so wm urgency hints can work from within screen) vbell off # Increase the scrollback buffer defscrollback 50000 screen -t root 0 su screen -t local 1 bash screen -t home 2 ssh -i .ssh/key.ppk -R 19999:localhost:22 username@x.x.x.x -p 443 select 0 # message display time (seconds) msgwait 2 

Screen Theme Changer Script

One of the first things you'll notice is that I binded C-a-m to a script: $HOME/bin/screen/screen_theme.sh

What this script does, is reload a new "caption always" and "hard status line" screen config so i can change the colors of my screen setup. This is relatively useful bc i tend to have 4-5 screen sessions going at a time. One will be for our email systems, another is for our SCM systems, another for our DNS infrastructure/smtp relays/snmp boxes, etc, another is for our web environment. Having different screen sessions look diffferent enables me, at a glance, to know which one i'm in. Here's that script:

 #!/bin/bash INDEXFILE="$HOME/bin/screen/themes/theme_index" # if this is the first time then set # the index to 0 if [[ ! -e $INDEXFILE ]] then echo 0 > $INDEXFILE fi THEMENO=`cat $INDEXFILE` THEMEMAX=5 if [[ $THEMENO -eq $THEMEMAX ]] then THEMENO=0 else THEMENO=`expr $THEMENO + 1` fi echo $THEMENO > $INDEXFILE THEMEFILE=$HOME/bin/screen/themes/theme${THEMENO} if [[ -e $THEMEFILE ]] then bash $THEMEFILE $STY else # reset the index back to zero if broken echo 0 > $INDEXFILE fi 

Themes

So it calls an index file and based on the number in that index file it switches to the next color. It does this by "theme files" which are basically just new screenrc's. They are in the directory $HOME/screen/themes as is the theme index.

 $ pwd /home/username/bin/screen/themes $ ls -a . .. theme0 theme1 theme2 theme3 theme4 theme5 theme_index 

theme0

 #!/bin/bash # yellow SESSION=$1 screen -S $SESSION -X caption always "%{= KW}%-w%{= Yk}%n %t%{-}%+w %-=" screen -S $SESSION -X hardstatus alwayslastline "%{= kW} %-= %{= kY}Session: %u%{= kW}%5\` | %{= kY}Host:%{= kW} %H | %{= kY} Uptime:%{= kW} %4\` | %{= kY} MEM:%{= kW} %2\`MB |%{= kY} SW: %{= kW}%3\`MB | %{= kY} DATE:%{= kW} %m/%d/%y %C %A" 

theme1

 #!/bin/bash # green SESSION=$1 screen -S $SESSION -X caption always "%{= KW}%-w%{= Gk}%n %t%{-}%+w %-=" screen -S $SESSION -X hardstatus alwayslastline "%{= kW} %-= %{= kG}Session: %u%{= kW}%5\` | %{= kG}Host:%{= kW} %H | %{= kG} Uptime:%{= kW} %4\` | %{= kG} MEM:%{= kW} %2\`MB |%{= kG} SW: %{= kW}%3\`MB | %{= kG} DATE:%{= kW} %m/%d/%y %C %A" 

theme2

 #!/bin/bash # red SESSION=$1 screen -S $SESSION -X caption always "%{= KW}%-w%{= Rk}%n %t%{-}%+w %-=" screen -S $SESSION -X hardstatus alwayslastline "%{= kW} %-= %{= kR}Session: %u%{= kW}%5\` | %{= kR}Host:%{= kW} %H | %{= kR} Uptime:%{= kW} %4\` | %{= kR} MEM:%{= kW} %2\`MB |%{= kR} SW: %{= kW}%3\`MB | %{= kR} DATE:%{= kW} %m/%d/%y %C %A" 

theme3

 #!/bin/bash # blue SESSION=$1 screen -S $SESSION -X caption always "%{= KW}%-w%{= bw}%n %t%{-}%+w %-=" screen -S $SESSION -X hardstatus alwayslastline "%{= kW} %-= %{= kB}Session: %u%{= kW}%5\` | %{= kB}Host:%{= kW} %H | %{= kB} Uptime:%{= kW} %4\` | %{= kB} MEM:%{= kW} %2\`MB |%{= kB} SW: %{= kW}%3\`MB | %{= kB} DATE:%{= kW} %m/%d/%y %C %A" 

theme4

 #!/bin/bash # magenta SESSION=$1 screen -S $SESSION -X caption always "%{= KW}%-w%{= mw}%n %t%{-}%+w %-=" screen -S $SESSION -X hardstatus alwayslastline "%{= kW} %-= %{= kM}Session: %u%{= kW}%5\` | %{= kM}Host:%{= kW} %H | %{= kM} Uptime:%{= kW} %4\` | %{= kM} MEM:%{= kW} %2\`MB |%{= kM} SW: %{= kW}%3\`MB | %{= kM} DATE:%{= kW} %m/%d/%y %C %A" 

theme5

 #!/bin/bash # cyan SESSION=$1 screen -S $SESSION -X caption always "%{= KW}%-w%{= Ck}%n %t%{-}%+w %-=" screen -S $SESSION -X hardstatus alwayslastline "%{= kW} %-= %{= kC}Session: %u%{= kW}%5\` | %{= kC}Host:%{= kW} %H | %{= kC} Uptime:%{= kW} %4\` | %{= kC} MEM:%{= kW} %2\`MB |%{= kC} SW: %{= kW}%3\`MB | %{= kC} DATE:%{= kW} %m/%d/%y %C %A" 

BACKTICK SCRIPTS FOR STATS

Ok - and now you notice the backticks up there in the .screenrc. These define the numbers of scripts in screenrc. Then these numbers are referenced on the hardstatus line.

They are as follows:

backtick 2 60 60 $HOME/bin/screen/get_freemem

 #!/bin/bash free -m | grep - | awk '{print $4}' 

backtick 3 60 60 $HOME/bin/screen/get_freeswap

 #!/bin/bash free -m | grep Swap | awk '{print $4}' 

backtick 4 60 60 $HOME/bin/screen/get_uptime

 #!/bin/sh uptime | awk '{print $3, $4}' | cut -d, -f1 

backtick 5 60 60 $HOME/bin/screen/get_sessionname

 #!/bin/sh screen -ls | grep $PPID | awk '{print $1}' | cut -f2 -d'.' 

backtick 5 60 60 $HOME/bin/screen/get_volume PCM

 #!/bin/bash status=$(amixer sget $1 | tail -n 1) vol=$(echo ${status} | gawk '{print $5}' | tr -d [%]) state=$(echo ${status} | gawk '{print $7}') text="${vol}" echo ${text} 

backtick 6 60 60 $HOME/bin/screen/get_gmail

 #!/bin/bash gmail_login="username" gmail_password="password" dane="$(wget --secure-protocol=TLSv1 --timeout=3 -t 1 -q -O - https://username:password@mail.google.com/mail/feed/atom --no-check-certificate )" line="$(echo "$dane" | grep 'fullcount')" gmail="$(echo "$line" | sed "s/<fullcount>\(.*\)<\/fullcount>/\1/")" if [ -z "$gmail" ]; then echo "!" else echo "$gmail" fi 

backtick 7 60 60 $HOME/bin/screen/get_updates

This was for arch - and i have also sense lost the script.

AUTO NAME SCREEN WINDOWS AFTER HOSTNAME YOU SSH TO

What's more - when I ssh to a box, my screen window title automatically changes to the IP or the hostname I ssh'd to. This is how this is done:

in my $HOME/.ssh/config

 LocalCommand ~/bin/screen_ssh.sh $PPID %n PermitLocalCommand yes 

The script in question:

 #!/bin/bash # screen_ssh.sh by Chris Jones <cmsj@tenshu.net> # Released under the GPL v2 licence. # Set the title of the current screen to the hostname being ssh'd to # # usage: screen_ssh.sh $PPID hostname # # This is intended to be called by ssh(1) as a LocalCommand. # For example, put this in ~/.ssh/config: # # Host * # LocalCommand /path/to/screen_ssh.sh $PPID %n # If it's not working and you want to know why, set DEBUG to 1 and check the # logfile. DEBUG=1 DEBUGLOG="$HOME/.ssh/screen_ssh.log" set -e set -u dbg () { if [ "$DEBUG" -gt 0 ]; then echo "$(date) :: $*" >>$DEBUGLOG fi } dbg "$0 $*" # We only care if we are in a terminal tty -s # We also only care if we are in screen, which we infer by $TERM starting # with "screen" if [ "${TERM:0:6}" != "screen" ]; then #if [ "${TERM:0:6}" != "xterm-" ]; then dbg "Not a screen session, ${TERM:0:5} != 'screen'" exit fi # We must be given two arguments - our parent process and a hostname # (which may be "%n" if we are being called by an older SSH) if [ $# != "2" ]; then dbg "Not given enough arguments (must have PPID and hostname)" exit fi # We don't want to do anything if BatchMode is on, since it means # we're not an interactive shell set +e grep -a -i "Batchmode yes" /proc/$1/cmdline >/dev/null 2>&1 RETVAL=$? if [ "$RETVAL" -eq "0" ]; then dbg "SSH is being used in Batch mode, exiting because this is probably an auto-complete or similar" exit fi set -e # Infer which version of SSH called us, and use an appropriate method # to find the hostname if [ "$2" = "%n" ]; then HOST=$(xargs -0 < /proc/$1/cmdline) dbg "Using OpenSSH 4.x hostname guess: $HOST" else HOST="$2" dbg "Using OpenSSH 5.x hostname specification: $HOST" fi if [[ $(echo "$HOST" | grep "[A-Za-z]") ]] then echo $HOST | sed -e 's/\.[^.]*\.[^.]*\(\.uk\)\{0,1\}$//' | awk '{ printf ("\033k%s\033\\", $NF) }' else echo $HOST | awk '{ printf ("\033k%s\033\\", $NF) }' fi #echo $HOST | sed -e 's/\.[^.]*\.[^.]*\(\.uk\)\{0,1\}$//' | awk '{ printf ("\033k%s\033\\", $NF) }' dbg "Done." 

SSH TAB AUTOCOMPLETE

And lastly - turn on auto-completion for ssh:

Add this to your $HOME/.bashrc

 # Autocomplete Hostnames for SSH etc. # by Jean-Sebastien Morisset (http://surniaulula.com/) _complete_hosts () { COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" host_list=`{ for c in /etc/ssh_config /etc/ssh/ssh_config ~/.ssh/config do [ -r $c ] && sed -n -e 's/^Host[[:space:]]//p' -e 's/^[[:space:]]*HostName[[:space:]]//p' $c done for k in /etc/ssh_known_hosts /etc/ssh/ssh_known_hosts ~/.ssh/known_hosts do [ -r $k ] && egrep -v '^[#\[]' $k|cut -f 1 -d ' '|sed -e 's/[,:].*//g' done sed -n -e 's/^[0-9][0-9\.]*//p' /etc/hosts; }|tr ' ' '\n'|grep -v '*'` COMPREPLY=( $(compgen -W "${host_list}" -- $cur)) return 0 } complete -F _complete_hosts ssh complete -F _complete_hosts host 

That will pull hosts from your $HOME/.ssh/known_hosts and /etc/hosts

Just setup ssh key everywhere, and working from the terminal is a golden experience.

Edit: Pic: http://i.imgur.com/kqKMLRm.png

while that shows what it looks like by default, the beauty is it's usefulness. Especially all the ssh stuff.

submitted by trioxinhardbodies
[link][16 comments]

Viewing all articles
Browse latest Browse all 18193

Trending Articles