Source

dotfiles / .myshrc

#!/bin/sh

set -o vi
stty erase 
stty -ixon

export SYMCLI_FULL_PDEVNAME=1
export PYTHONPATH=/home/yunake/pythonlibs
export _JAVA_OPTIONS='-Dawt.useSystemAAFontSettings=on -Dswing.aatext=true -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel'
export PASSWD_FILE=/home/yunake/.ssh/lsearchpw

if type less > /dev/null 2>&1; then
  export PAGER=less
  export LESS='FSRX'
else
  export PAGER=more
  alias less=more
fi

if type vim > /dev/null 2>&1; then
  export EDITOR=vim
  export VISUAL=vim
  export FCEDIT=vim
  alias vi=vim
else
  export EDITOR=vi
  export VISUAL=vi
  export FCEDIT=vi
  alias vim=vi
fi

case $LOGNAME in
  root) sudo=""     ;;
  *)    sudo="sudo" ;;
esac

alias hs="$sudo hastatus -sum"
alias hl="$sudo less /var/VRTSvcs/log/engine_A.log"
alias vxdl="$sudo vxdisk -eo alldgs list"

case $(uname) in
  SunOS)
    alias ups='/usr/ucb/ps'
    alias psw='/usr/ucb/ps auxwww'
    alias awk=nawk
    export AWK=nawk
    alias G='grep'
    alias mess="$sudo less /var/adm/messages"
  ;;
  Linux)
    alias psw='ps auxwww'
    alias awk=gawk
    export AWK=gawk
    # TODO: rewrite this as function that sends output to less?
    alias G='grep --color=always'
    alias ls='/bin/ls --color=auto'
    alias LS='/bin/ls --color=always'
    if [ -e /var/log/messages ]; then
      alias mess="$sudo less /var/log/messages"
    elif [ -e /var/log/messages.log ]; then
      alias mess="$sudo less /var/log/messages.log"
    fi
    alias pac='sudo pacman-color'
  ;;
  OpenBSD)
    alias psw='ps auxwww'
    export AWK=awk
  ;;
  HP-UX)
    alias psw='UNIX95= ps -ef'
    export AWK=awk
  ;;
  *)
    alias psw='ps -ef'
    export AWK=awk
  ;;
esac

unset sudo

alias rm='rm -f'
alias mv='mv -f'
alias cp='cp -f'
alias L='less'

alias pw='getent passwd'

function ng
{
  # TODO: make this work on Solaris
  # TODO: add option to show description and authoriser
  for usr in $(getent netgroup $1 | sed -e 's/(/\
/g' -e 's/ , //g' -e 's/, bskyb.com)//g' | sed 1d)
  do
    getent passwd $usr | $AWK -F: '{printf "%8s  %s\n", $1, $5}'
  done
  unset usr
}

if type dircolors > /dev/null 2>&1; then
  eval `dircolors ~yunake/.ls_colors`
fi

if [ "z" != "z$TMUX" ]
then
  function ssh_tmux
  {
    tmux rename-window $1
    ssh -q "$@"
    tmux rename-window `hostname`
  }
  alias ssh='ssh_tmux'
else
  alias ssh='ssh -q'
fi

function lsearch
{
  # lsearch [-sd] [-h ldaplive] [-b ou=people] uid=smo12
  # -s means we need to add dc=sssl
  # if there is no -h, then default to amsldaps10, if -s then ldaplive
  # -d is "debug": print resulting command

  # BUGS:
  # Solaris: if the prev line = ^$ and this line is cn= change it to dn: and don't do s/cn=/cn:/

  # TODO:
  # add support for querying AD:
  # ldapsearch -x -h livdc04.bskyb.com -b 'dc=bskyb,dc=com' -D 'BSKYB\tan02' -CW "(objectGUID=\fb\f2\55\dd\92\69\13\47\ae\b4\58\75\35\de\02\86)"
  # colorise output

  while getopts "sdh:b:" opt
  do
    case $opt in
      s) sssl="true" ;;
      d) debug="true" ;;
      h) host="$OPTARG" ;;
      b) base="$OPTARG" ;;
      *) echo "Usage: lsearch [-sd] [-h ldaplive] [-b ou=people] uid=smo12" && return 1;;
    esac
  done
  shift $(($OPTIND-1))

  # is stdout a terminal?
  if [ -t 1 ]; then term=1; else term=0; fi

  # OS-specific settings
  # awk is needed to make the output look the same regardless of the OS - ":" is used as separator
  # we also use it to colorise the output on terminals and undo wrap at 72 cols on Linux
  # TODO: Solaris 9 has this nasty "split" in ldif-format too, like linux and unlike 5.10
  case $(uname) in
    SunOS)
      opts="-B"
      attrs=""
      awk='
        NR == 1 {
          sub (/^/, "dn: ");
          print;
        }
        NR != 1 {
          sub (/\=/, ": ");
          print;
        }
      '
    ;;
    Linux)
      opts="-xWLLL"
      attrs=""
      awk='
        /^ / {
          sub (/^ /, "");
          pline = pline$0;
          $0 = "::NOTHING HERE::";
        }
        {
          if (NR != 1 && 
           pline != "::NOTHING HERE::") {
            num = split (pline, arr);
            if (tolower(arr[1]) == "userpassword::") {
              sub (/::/, ":", arr[1]);
              cmd = sprintf ("echo -n %s | base64 -d", arr[2]);
              cmd | getline arr[2];
            }
            for (i=1; i<=num; i++)
              str = sprintf ("%s %s", str, arr[i]);
            sub (/^ /, "", str);
            print str;
            str = "";
          }
          pline = $0;
        }
      '
    ;;
    *)
      echo "Not tested on your platform, sorry. Patches are welcomed!"
      return 1
    ;;
  esac

  # if base is not set explisitly
  if [ "z" = "z${base}" ]; then
    isou=$(echo $1 | egrep 'ou=|o=')
    # if we have many arguments, assume first one is the base
    if [ $# -gt 1 -a "z" != "z${isou}" ]; then
      base="$1"
      shift
    # if we only have one argument
    else
      base="ou=people"
    fi
    unset isou
  fi
  if [ $# -eq 0 ]; then
    echo "filter not set. add e.g. uid=smo12; you can also add specific attributes as second arg" && return 1
  fi
  filter="$1"
  shift
  # if we have more than one arg, add all of them to the command line and omit linux default "+" attr
  if [ $# -gt 0 ]; then
    attrs="$@"
  fi

  # figure out the correct host to query
  if [ "z" = "z${host}" ]; then
    if [ "z" = "z${sssl}" ]; then
      host="amsldaps10"
    else
      host="ldaplive"
    fi
  fi

  if [ "z" = "z${sssl}" ]; then
    hbase="dc=bskyb,dc=com"
  else
    hbase="dc=sssl,dc=bskyb,dc=com"
  fi

  # use password file if it's not empty
  homedir=$(ls -d ~yunake)
  if [ -s $homedir/.ssh/lsearchpw ]; then
    case $(uname) in
      SunOS) opts="$opts -j $homedir/.ssh/lsearchpw" ;;
      Linux) opts="$opts -y $homedir/.ssh/lsearchpw" ;;
    esac
  fi

  # finally, perform the search
  if [ "z" != "z${debug}" ]; then
    echo ldapsearch $opts -h $host -D \""uid=yunake,ou=people,$hbase"\" -b \""$base,$hbase"\" \""$filter"\" $attrs \| $AWK -v \""term=$term"\" \'"$awk"\' >&2
  fi
  ldapsearch $opts -h $host -D "uid=yunake,ou=people,$hbase" -b "$base,$hbase" "$filter" $attrs | $AWK -v "term=$term" "$awk"

  # cleanup
  unset opts attrs base sssl host filter hbase homedir awk debug term
}

function ngs
{
  # TODO: make the function work with multiple arguments and also read stdin if there are no arguments.
  while getopts "sh:" opt
  do
    case $opt in
      s) opts="$opts -s" ;;
      h) opts="$opts -h $OPTARG" ;;
      *) echo "Usage: ngs [-s] [-h ldaplive] smo12" && return 1;;
    esac
  done
  shift $(($OPTIND-1))

  lsearch $opts -b 'ou=netgroup' "nisNetgroupTriple=\(,$1,bskyb.com\)" | $AWK '-F ' '$1 == "cn:" && NF == 2 { print $2 }'
  unset opts
}

function sur
{
  # a better su :) replaces alias root, too
  touser=${1:-'root'}
  if [ $LOGNAME = 'root' ]; then
    sudo=""
  else
    sudo='sudo'
  fi
  $sudo su - $touser -c '/usr/bin/env ENV=~yunake/.kshrc ksh -o vi'
  unset sudo touser
}
alias root='sur root'

function hostlocation
{
  hostname=${1:-$(hostname)}
  longname=$(nslookup -q=txt $hostname | $AWK -F= '/text/ {print $NF}' | sed 's/-pub"/-rib/')
  if [ "z" = "z$longname" ]; then
    code=$(echo $hostname | cut -c1-3)
    longname="${hostname}-rib"
  else
    code=$(echo $longname | cut -c2,3)
  fi
  if nslookup $longname | grep Name >/dev/null 2>&1; then
    longname=":$longname"
  else
    longname=""
  fi
  echo $($AWK -F: -v "code=$code" '$1 == code || $2 == code || $6 == code {print $3}' /share/bbscripts/PRD/Host/Redhat/Deploy/WhatAmI/LocationMatrix)$longname
  unset hostname longname code
}
alias hostl=hostlocation

function hostbyrib
{
  # use with either a ribname, ribfqdn or assettag, like this:
  # hostbyrib onx0113025
  # hostbyrib sce0136591.bskyb.com
  # hostbyrib 113025
  # in fact, any string with 7 digits will do

  [ $# -ne 1 ] && echo "Incorrect argument" && return 1

  tag=$(echo "$1" | sed 's/^.*\([0-9]\{7\}\).*$/\1/')
  [ $(echo $tag | wc -c) -ne 8 ] && echo "Number of digits in asset tag does not equal to 7" && return 1

  if [ -s ~yunake/.ssh/lsearchpw ]; then
    sparkpassword=$(cat ~yunake/.ssh/lsearchpw)
  else
    echo -n "Your AD password: "
    stty -echo
    read sparkpassword
    stty echo
    echo
  fi

  curl -sS "https://$LOGNAME:$sparkpassword@bskyb.service-now.com/cmdb_ci_server_list.do?JSON&sysparm_action=getRecords&sysparm_query=asset_tag=$tag" | sed 's/,/\
/g' | $AWK -F: '/"name"|"u_esm_access_name"/ { print $2 }' | sed 's/"//g'

  unset tag sparkpassword
}
alias hostr=hostbyrib

function hw
{
  case $(uname) in
    SunOS)
      /usr/platform/$(uname -i)/sbin/prtdiag | sed -n "1s/^.*$(uname -m) //; 1p"
    ;;
    Linux)
      dmi=$(sudo dmidecode | sed -n '/System Information/,/^$/p')
      manufacturer=$(echo "$dmi" | grep 'Manufacturer:' | sed 's/^.*Manufacturer: //')
      product=$(echo "$dmi" | grep 'Product Name:' | sed 's/^.*Product Name: //')
      version=$(echo "$dmi" | grep 'Version:' | sed 's/^.*Version: //')
      echo "$manufacturer $product $version"
      unset dmi manufacturer product version
    ;;
    OpenBSD)
      sysctl hw.model | awk -F= '{print $2}'
    ;;
    *)
      echo "Not implemented" && return 1
    ;;
  esac
}

function setuprib
{
  if [ "$(hw | awk '{print $1}')" != "HP" -o
       "z" = "z$(hostl | awk '{print $2}')"]
  then
    echo "we are not a valid platform, aborting"
    return 1
  fi
  if sudo grep -q "S0:2345:respawn:/sbin/agetty" /etc/inittab; then
     echo "RIB was already set up, aborting"
     return 1;
  fi
  sudo sh -c 'echo "S0:2345:respawn:/sbin/agetty 115200 ttyS0 vt100" >>/etc/inittab'
  sudo sh -c 'echo "setserial /dev/ttyS0 uart 16550A port 0x0408 irq 4" >/etc/rc.serial'
  sudo chmod 755 /etc/rc.serial
  sudo sh -c 'echo "/etc/rc.serial" >>/etc/rc.local'
  sudo sh -c 'echo "ttyS0" >>/etc/securetty'
  sudo sed -i '/^[	 ]*kernel/ s/$/ console=ttyS0/' /boot/grub/menu.lst
  sudo kill -15 1
}

function sshrib
{
  # TODO: handle no-rib-found in hostl
  # TODO: determine OS, use admin for solaris + different msg
  # TODO: support ssh through Raritan and VSphere
  echo "Use command \"VSP\" to access serial console"
  echo "Enter CTRL+V, ESC, ( to close session"
  ssh $(hostl $1 | awk -F: '{print $2}') -l admin-linux
}