view .bash_aliases @ 285:f62fb9579277

Changing irc alias a little due to how docker exits; update histignore
author Steve Huston <huston@srhuston.net>
date Tue, 16 Jul 2024 22:22:44 -0400
parents 2b513df62dd4
children b7096eec6557
line wrap: on
line source

# Aliases are read from this file; no real reason to separate them out except
# for readability.

#
# Some settings which are architecture dependent
#
case `uname` in
  Darwin)
    # DARWIN is used for the SSHFS mounts below
    DARWIN="yes"

    # The 'stat' and 'date' commands are here since the syntax of the commands
    # is different between Linux and Mac OS X.  If this is run elsewhere, the
    # eval will be false and the test not done below, so it's safe to not
    # bother checking for zero-length variables on the test - it just won't
    # run the hg incoming, so you won't be prompted if there's newer files
    # available in your repository.  These are safe to export since they're
    # the commands to be evaulated, not the output - you're storing how to get
    # the answer, not the answer itself.

    # Also note, the full paths are stored since GNU coreutils might be
    # installed; while that would tend to make things "easier" since the GNU
    # one is the same as the Linux ones below, it can't be guaranteed.
    # Instead of checking for the presence of /opt/local/bin/ls (or gls) it's
    # easier to point directly to the one you know *is* installed.

    DFSTAT="/usr/bin/stat -f %m -t %s $HOME/.dotfilets"
    MDATE="/bin/date -v -1m +%s"

    alias ls='/bin/ls -FG'

    VNCVIEWER="open vnc://"
    ;;

  Linux)
    # See above for a description of these four
    DFSTAT="/usr/bin/stat -c %Y $HOME/.dotfilets"
    MDATE="/bin/date -d \"1 month ago\" +%s"

    alias ls='/bin/ls --color=auto -F'

    VNCVIEWER="vncviewer Shared=1 Fullcolor=1 "
    ;;
esac

#
# Export some of those above for the benefit of future shells
#
export DFSTAT MDATE

#
# Create a service that can share files through to places we ssh to from here.
# This should be pretty light, rooted in the $DOTHOME directory (see ~/.bashrc
# for information) and removed when we either request it or the shell exits.
#
ds() {

  PIDFILE=${DOTHOME}/.dotshare-PID

  # If we want to kill an existing server, let's do that first.
  if [ "$1" == "end" ]; then
    [[ -e $PIDFILE ]] || {
      echo "No server detected"
      return 1
    }
    kill `cat $PIDFILE`
    unset DOTSHARE_PID DOTSHARE_PORT
    echo "Server terminated"
    return 0
  fi

  # Let's check if things exist and we can just use them as-is
  if [ -e $PIDFILE ]; then
    DOTSHARE_PID=`cat $PIDFILE`
    DOTSHARE_PORT=`lsof -w -n -p $DOTSHARE_PID | grep LISTEN | awk '{print $9}' | sed 's/.*://'`
    if [ -z $DOTSHARE_PORT ]; then
      echo "Server PIDfile found but couldn't get a port, aborting"
      return 255
    fi
    echo "Existing server ${DOTSHARE_PID} listening on ${DOTSHARE_PORT}, setting environment"
    export DOTSHARE_PID DOTSHARE_PORT
    return 0
  fi

  # Nothing exists, so we need to make one; first, which python do we have
  if hash python3 > /dev/null 2>&1 ; then
    # Python 3.9 also accepts '-d' for a directory
    serv_cmd="python3 -m http.server --bind localhost 0"
  elif hash python > /dev/null 2>&1 ; then
    # I think nothing else where I'd run this will need this one
    serv_cmd="python -m SimpleHTTPServer 0"
  else
    echo "No python found, cannot start share server."
    return 255
  fi

  # Spawning this in a subshell because the process won't go away and we want
  # to background it, but we also want to set a trap around the whole thing,
  # and we need to 'cd' to somewhere else to start it since older versions
  # can't be passed a different path to serve.  So there's some hacks here,
  # but I don't think they're avoidable for the moment.  Once Joshua and
  # Xanadu have Python 3.9 at least, I can rework this whole thing.
  (
    cd $DOTHOME
    $serv_cmd > /dev/null 2>&1 &
    P=$!
    echo $P > ${DOTHOME}/.dotshare-PID
    trap "kill $P 2>/dev/null; rm -f $PIDFILE; unset DOTSHARE_PID DOTSHARE_PORT" 0
    wait
  ) &
  disown %+

  # Give it a moment to collect its thoughts...
  sleep 1
  DOTSHARE_PID=`cat $PIDFILE`
  DOTSHARE_PORT=`lsof -w -n -p $DOTSHARE_PID | grep LISTEN | awk '{print $9}' | sed 's/.*://'`
  echo "Server PID $DOTSHARE_PID ready on port $DOTSHARE_PORT"
  export DOTSHARE_PID DOTSHARE_PORT
}

#
# SSH aliases/functions
#
cert() {
  # SSH key signing with Vault for administration
  export VAULT_ADDR='https://ajax.rc.princeton.edu:8200'
  ssh-add -d $HOME/.ssh/rc_vault_key-cert.pub > /dev/null 2>&1
  if vault login -method=radius username=vi-srh; then
    vault write -field=signed_key ssh-client-signer/sign/root public_key=@$HOME/.ssh/rc_vault_key.pub > $HOME/.ssh/rc_vault_key-cert.pub
    ssh-add -t 36000 $HOME/.ssh/rc_vault_key
  else
    echo Failed to login to vault, aborting
  fi
}

# I rename my keys from the standard so they're easy to glob on a commandline
alias keys='ssh-add $HOME/.ssh/*.pvt'

#
# These are for reattaching screen sessions, only useful on bastion systems
#
alias rbh='ssh -t csesbh2.princeton.edu screen -raAx csesbh2'
alias rj='ssh -t joshua.srhuston.net screen -raAx'
alias irc='ssh -t joshua.srhuston.net docker exec -it irssi screen -raAx;clear;echo IRC Session ended'
alias rx='ssh -t xanadu.astro.princeton.edu screen -raAx'

t() {
  # Tunneling SSH - a simple 't hostname' will tunnel to xanadu and then
  # connect to hostname, while 't hostname tunnelhost' will connect to
  # tunnelhost and then hostname
  # Today (2024/03/30) I don't remember the last time I used this; retire?
  T_HOST=xanadu.astro.princeton.edu
  if [ -n "$2" ] ; then
    T_HOST=$2
  fi
  ssh -t $T_HOST "ssh $1"
}

proxy() {
  # Proxying/tunneling - predominantly for my Mac laptop
  local PROXY_HOST PROXY_PORT PROXY_PATH PROXY_UMOUNT PROXY_CMD
  # We set a simple command to run over the proxy for normal connections,
  # which we know will be multiplexed and stay active.  If you're using a
  # manual host for something, you'll want to use a "sleep 30" or something to
  # keep the shell open long enough for the proxy to be used, or maybe even
  # longer if you plan on disconnecting and reconnecting over time.
  PROXY_CMD=${PROXY_CMD:-hostname}
  case $1 in
    home)
      if [ "$HOST" != "gallifrey" ] ; then
        echo "Not on gallifrey, no need for this"
        return
      fi
      PROXY_HOST=${PROXY_HOST:-joshua.srhuston.net}
      PROXY_PORT=${PROXY_PORT:-8889}
      PROXY_PATH="/Volumes/Chrome-Personal"
      PROXY_UMOUNT="hdiutil detach -quiet ${PROXY_PATH}"
      if [ ! -e ${PROXY_PATH}/.profile-exists ] ; then
        hdiutil attach -stdinpass -nobrowse -quiet ~/Documents/Chrome-Personal.dmg
        if [ ! $? ] ; then
          echo Disk image failed to mount, aborting
          return
        fi
      fi
      ;;
    work)
      PROXY_HOST=${PROXY_HOST:-xanadu.astro.princeton.edu}
      PROXY_PORT=${PROXY_PORT:-8888}
      if [ "$HOST" == "gallifrey" ] ; then
        PROXY_PATH="/Users/huston/Library/Application Support/Google/Chrome"
      else
        PROXY_PATH="/Users/huston/Library/Application Support/Google/Chrome_Work"
      fi
      ;;
    rc)
      PROXY_HOST=${PROXY_HOST:-csesbh2.princeton.edu}
      PROXY_PORT=${PROXY_PORT:-8887}
      PROXY_PATH="/Users/huston/Library/Application Support/Google/Chrome_RC"
      ;;
    w2zq)
      PROXY_HOST=${PROXY_HOST:-ssh://w2zq.mywire.org:22022}
      PROXY_PORT=${PROXY_PORT:-8886}
      PROXY_PATH="/Users/huston/Library/Application Support/Google/Chrome_W2ZQ"
      PROXY_UMOUNT="ssh -O exit $PROXY_HOST"
      ;;
    *)
      echo "Proxy needs one argument: home, work, rc, or w2zq"
      return
      ;;
  esac
  # In theory you need to have a job stay on the remote host for the
  # forwarding to stay open.  However if you're using persistent/shared
  # connections then the port stays open anyway.  We still need a command
  # though, and 'hostname' returns quickly as well as giving a visual output
  # that you connected to the right place.
  ssh -D $PROXY_PORT -f -C -q $PROXY_HOST "$PROXY_CMD"
  (
    /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
      --proxy-server="socks5://127.0.0.1:$PROXY_PORT" \
      --host-resolver-rules="MAP * 0.0.0.0 , EXCLUDE localhost" \
      --user-data-dir="${PROXY_PATH}" > /dev/null 2>&1
    $PROXY_UMOUNT
    # Maybe leave this off, so what if the port is left open if there are no
    # conflicts...
    # ssh -O cancel -D $PROXY_PORT $PROXY_HOST
  ) &
  disown %+
}

#
# Remote desktop/VNC - definitely outdated and maybe no longer useful?
#
eval "alias milton='ssh -L9930:milton:5900 -f joshua.srhuston.net \"sleep 5\"; ${VNCVIEWER}localhost:9930 &'"

#
# Programs
#
alias atari='atari800 -height 800 -width 600'
alias base64='openssl enc -a'
alias irssi-test='irssi --home=$HOME/irssi-test/'
alias pine='alpine'
alias ret='screen -raAx'
tt () {
  OPTS="-G"
  if [ -e ${HOME}/.tt/${1}.tin ] ; then
    OPTS="${OPTS} ${HOME}/.tt/${1}.tin"
    shift
  fi
  tt++ ${OPTS} $@
}

#
# SSHFS mounts
# The $DARWIN check fills in a nice volname if this is a Mac
#
alias junkpile="sshfs -oreconnect${DARWIN:+,volname=Junkpile} srhuston.net:junkpile $HOME/junkpile"
alias sdh="sshfs -oreconnect${DARWIN:+,volname=srhuston.net} srhuston.net: $HOME/srhuston.net"
alias sj="sshfs -oreconnect${DARWIN:+,volname=Joshua} joshua.srhuston.net: $HOME/joshua"
alias sx="sshfs -oreconnect${DARWIN:+,volname=Xanadu} xanadu.astro.princeton.edu: $HOME/xanadu"
alias vw="sshfs -oreconnect${DARWIN:+,volname=VW} vw.math.princeton.edu: $HOME/vw"
alias tmu="sshfs -oreconnect${DARWIN:+,volname=tomenet-user} joshua.srhuston.net:Installs/tomenet/lib/user $HOME/Installs/tomenet/lib/user"

#
# LDAP/host tools
#
alias ldm="ldapmodify -Q -Y GSSAPI -c"
alias ldsloop='while true; do read; clear; lds "$REPLY"; done'
lds () {
  QUERY="$1"
  shift
  OPTS=""
  [[ "$1" =~ ^- ]] && {
    OPTS="$1"
    shift
  }
  ldapsearch -Q -Y GSSAPI -LLL $OPTS "($QUERY)" $@
}

#
# Other
#
hgs() {
  # Creates a hg server in the current repo and connects to it automatically.
  # We assume here that if the host is a Mac, we're going to open the
  # connection automatically, otherwise we echo the URL to the terminal to be
  # opened
  ROOT=`hg root`
  if [ "$1" == "end" -a -e $ROOT/.hg/hgserve-PID ] ; then
    kill `cat $ROOT/.hg/hgserve-PID`
    rm $ROOT/.hg/hgserve-PID
  else
    if [ -e $ROOT/.hg/hgserve-PID ] ; then
      P=`cat $ROOT/.hg/hgserve-PID`
      URL=`lsof -w -n -p $P | grep LISTEN | awk '{print $9}'`
    else
      URL=`hg serve -d -p 0 --pid-file $ROOT/.hg/hgserve-PID | sed 's#.*at http://\(.*\)/ (bound.*#\1#'`
    fi
    if [ -z "$DARWIN" ] ; then
      echo hg serve on http://$URL/
    else
      open http://$URL/
    fi
  fi
}
mc-backup() {
  # Definitely time to rework this one - I don't even use Dropbox anymore!
  # Keeping it here for now just for the syntax, but this is probably no
  # longer worth keeping either.
  pushd ~/Dropbox/Saves/minecraft/saves;
  if [ -d $1 ]; then
    tar cvf - $1 | bzip2 > ../$1_`date +%Y%m%d%H%M%S`.tar.bz2;
  fi;
  popd
}
alias slurp="wget -r -l1 -np -nd -A.mp3"
alias zulu="date -u +%Y%m%d%H%M%SZ"
TOhtml() {
  # Since I moved to hosting the hg repo, this is less useful to share things.
  # But still a nice bit of code and worth keeping I think.  Needs a rework to
  # match the new style of the dotfile repo as of 2024/03/30 (splitting out
  # shared configs, etc)
  OUTDIR=${1:-`mktemp -d TOHTML.XXXXXX`}
  ROOT=`hg root`

  echo "<HTML><HEAD><TITLE>My dotfiles</TITLE></HEAD><BODY><UL>" > $OUTDIR/index.html

  for F in `hg locate` ; do

    # Files to ignore from the list
    case $F in
      .vim/*)
        continue
        ;;
      *)
        ;;
    esac

    D=`echo $F | grep "/" | sed 's/\/[^\/]*$//'`
    if [ $D ] ; then
      mkdir -p $OUTDIR/$D
    fi

    vim -n -e +TOhtml "+w ${OUTDIR}/${F}.html" +qa\! $ROOT/$F
    echo "<LI><A href=${F}.html>$F</A></LI>" >> $OUTDIR/index.html
  done
  echo "</UL></BODY></HTML>" >> $OUTDIR/index.html
  echo Files stored in $OUTDIR
}

# vim: set filetype=sh :