record bash history from some remote hosts
authorIan Kelling <ian@iankelling.org>
Tue, 24 Feb 2026 07:56:57 +0000 (02:56 -0500)
committerIan Kelling <ian@iankelling.org>
Tue, 24 Feb 2026 08:01:07 +0000 (03:01 -0500)
brc

diff --git a/brc b/brc
index 8a7daebe584dcb99c8611e7b989caf3bd0c5eb99..c62b940c57177d4c47a997205735da07f0b4930a 100644 (file)
--- a/brc
+++ b/brc
@@ -3164,12 +3164,14 @@ _sl-get-host-type() {
     if [[ $host_type == skip ]]; then return; fi
   fi
 
-  sshinfo_files=($SL_INFO_DIR/??????????-????-"$remote")
+  ngset
+  sshinfo_files=( $SL_INFO_DIR/??????????-????-"$remote" )
   # there should only be one, but just in case, remove all but 1.
   if (( ${#sshinfo_files[@]} >= 2 )); then
     mq rm ${sshinfo_files[@]::${#sshinfo_files[@]}-1}
-    sshinfo_files=($SL_INFO_DIR/??????????-????-"$remote")
+    sshinfo_files=( $SL_INFO_DIR/??????????-????-"$remote" )
   fi
+  ngreset
   sshinfo="${sshinfo_files[0]}"
 
   # if $verbose; then e "sshinfo: '$sshinfo'"; fi # debug
@@ -3415,6 +3417,10 @@ sl() {
   if [[ $host_type == sync ]] && ! $pre_synced; then
     m RSYNC_RSH="ssh ${args[*]}" $sl_rsync_cmd -rptL --delete $SL_FILES_DIR "$remote":
     mq rm-maybe "$sshinfo"
+    sshinfo=
+  fi
+
+  if [[ ! $sshinfo ]]; then
     sshinfo=$SL_INFO_DIR/$EPOCHSECONDS-$host_type-"$remote"
     [[ -e $SL_INFO_DIR ]] || mkdir -p $SL_INFO_DIR
     printf "%s\n" "$extra_info" >$sshinfo
@@ -3438,11 +3444,15 @@ sl() {
       # But, since we are doing all this, lets source it because we can.
       cat <(echo . $sync_dirname/.bashrc) - | command ssh "${args[@]}" "$remote" LC_USEBASHRC=t bash
     else
+      # random deletable note: This ssh corresponds to the one below
+      # with reverse tunnel below, and it would make sense to add it
+      # here too, but I'm mainly interested in collecting history from
+      # skip type hosts,so leaving it out for now.
       mq command ssh -t "${args[@]}" "$remote" LC_USEBASHRC=t INPUTRC=$sync_dirname/.inputrc bash --rcfile $sync_dirname/.bashrc
     fi
   else
     if [[ -t 0 ]]; then
-      mq LC_USEBASHRC=t command ssh "${args[@]}" "$remote" "$@"
+      mq LC_USEBASHRC=t command ssh -R localhost:36660:localhost:36661 "${args[@]}" "$remote" "$@"
     else
       mq command ssh "${args[@]}" "$remote" LC_USEBASHRC=t bash
     fi
@@ -3455,6 +3465,23 @@ sl() {
 # output if it took longer than 4 seconds or so.
 # todo: try running slowdo on verbose ssh
 
+# Stores some history from remote ssh sessions via sl.
+hist-catcher() {
+  local cmd
+  local cmd_regex='[[:alnum:]]'
+
+  while true; do
+    cmd=$(nc -l 36661) ||:
+    e "cmd:'$cmd'";
+    if [[ ! $cmd =~ $cmd_regex ]]; then
+      continue
+    fi
+    history -s "$cmd" && history -a ||:
+  done
+}
+# dev notes: useful: https://www.baeldung.com/linux/local-data-remote-ssh-session#:~:text=%23%23%23%205.2%5C.%20Netcat,provide%20data%20back%2A%2A.
+# TODO: automate starting/restarting this.
+
 
 slr() {
   sl --rsync "$@"
@@ -3998,7 +4025,11 @@ if [[ $- == *i* ]]; then
 
     if [[ $HISTFILE ]]; then
       # save history. if our filesystem went read-only or something, continue on
-      history -a ||:
+      if history -a; then
+        if [[ $SSH_CLIENT && $NOT_MAIL_HOST_P == t ]]; then
+          history 1 | sed -r 's/^([^ ]+ +){4}//' |  nc -N localhost 36660 ||:
+        fi
+      fi
 
 
       # if we aren't in ssh, maybe indicate history file