diff .bash_aliases @ 265:df0b24d4fabd

Think I'm ready to check this all in now; shared dotfiles that can be "sent" through an SSH connection to be used and cleaned up when finished.
author Steve Huston <huston@srhuston.net>
date Wed, 24 Apr 2024 16:15:40 -0400
parents b00e6b403896
children 48b4f41712dc
line wrap: on
line diff
--- a/.bash_aliases	Thu Apr 18 13:37:00 2024 -0400
+++ b/.bash_aliases	Wed Apr 24 16:15:40 2024 -0400
@@ -1,7 +1,9 @@
 # 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
@@ -41,11 +43,80 @@
     ;;
 esac
 
+#
 # Export some of those above for the benefit of future shells
+#
 export DFSTAT MDATE
 
-# SSH aliases
-alias bh='ssh csesbh2.princeton.edu'
+#
+# 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" -a -e $PIDFILE ]; then
+    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 -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 -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'
@@ -57,31 +128,38 @@
     echo Failed to login to vault, aborting
   fi
 }
-alias dh='ssh srhuston.net'
-alias j='ssh joshua.srhuston.net'
+
+# I rename my keys from the standard so they're easy to glob on a commandline
 alias keys='ssh-add $HOME/.ssh/*.pvt'
-alias r='ssh -l root'
-alias rf='ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o GlobalKnownHostsFile=/dev/null -l root'
+
+#
+# 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 rx='ssh -t xanadu.astro.princeton.edu screen -raAx'
-alias s=ssh
-alias sf='ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o GlobalKnownHostsFile=/dev/null'
+
 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"
 }
-alias x='ssh xanadu.astro.princeton.edu'
 
-# Proxying/tunneling - predominantly for my Mac laptop
 proxy() {
-  local PROXY_HOST PROXY_PORT PROXY_PATH PROXY_UMOUNT
+  # 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
@@ -130,7 +208,7 @@
   # 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 'hostname'
+  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" \
@@ -144,16 +222,19 @@
   disown %+
 }
 
-# Remote desktop/VNC
+#
+# 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'
-alias rot13="tr 'a-zA-Z' 'n-za-mN-ZA-M'"
 tt () {
   OPTS="-G"
   if [ -e ${HOME}/.tt/${1}.tin ] ; then
@@ -162,10 +243,11 @@
   fi
   tt++ ${OPTS} $@
 }
-alias vi='vim'
 
+#
 # 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"
@@ -173,10 +255,11 @@
 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
@@ -188,33 +271,49 @@
   ldapsearch -Q -Y GSSAPI -LLL $OPTS "($QUERY)" $@
 }
 
+#
 # Other
-alias slurp="wget -r -l1 -np -nd -A.mp3"
-alias zulu="date -u +%Y%m%d%H%M%SZ"
-
-# Other Functions
-
-certcheck() {
-  if [ -z "$1" ] ; then
-    echo "Usage: certcheck <hostname>[:port]"
-    echo "       Defaults to port 443 if not specified."
-    return
+#
+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 -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
-  H=$1
-  [[ $1 =~ :[0-9]+$ ]] || \
-    H=$1":443"
-  echo -n Q | openssl s_client -connect $H | openssl x509 -noout -dates
 }
-
 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`
 
@@ -243,27 +342,4 @@
   echo Files stored in $OUTDIR
 }
 
-hgs() {
-  # 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 -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
-}
-
-# vim: set filetype=sh :
+# vim: set filetype=bash :