Skip to content

Instantly share code, notes, and snippets.

@bartman
Created January 23, 2024 18:30
Show Gist options
  • Save bartman/5e823277f5e1f1f409bf2bf8d94d1b32 to your computer and use it in GitHub Desktop.
Save bartman/5e823277f5e1f1f409bf2bf8d94d1b32 to your computer and use it in GitHub Desktop.
netsonsole
#!/bin/bash
set -e
die() { echo >&2 "$@" ; exit 1 ; }
TGT_IP="$1"
TGT_PORT=${2:-6666}
if [ -z "$TGT_IP" ] ; then
TTY=${SSH_TTY#/dev/}
[ -n "$TTY" ] || die "IP not specified, could not get ssh TTY"
TGT_IP="$( who --ips | awk '$2 == "'"$TTY"'" { print $5 }' )"
[ -n "$TGT_IP" ] || die "IP not specified, could not figure it out"
echo >&2 "using target IP $TGT_IP"
fi
ping -q -W1 -c1 "$TGT_IP" >/dev/null 2>&1
VIA_IP="$(ip --oneline route get $TGT_IP | sed -r -n -e 's/.*via ([^ ]+) .*/\1/p;q')"
[ -n "$VIA_IP" ] && ping -q -W1 -c1 "$VIA_IP" >/dev/null 2>&1
TGT_MAC="$(ip --oneline neigh show "${VIA_IP:-$TGT_IP}" | awk '/REACHABLE|STALE|DELAY/{ print $5 }')"
SRC_IP="$(ip --oneline route get $TGT_IP | sed -rne 's/.*src //; s/ .*//p')"
SRC_DEV="$(ip --oneline route get $TGT_IP | sed -rne 's/.*dev //; s/ .*//p')"
echo "TGT_IP=$TGT_IP"
echo "VIA_IP=$VIA_IP"
echo "TGT_MAC=$TGT_MAC"
echo "SRC_IP=$SRC_IP"
echo "SRC_DEV=$SRC_DEV"
ID=$(id -u)
SUDO=
[ "$ID" = 0 ] || SUDO=sudo
if lsmod | grep -q netconsole ; then
$SUDO rmmod netconsole
fi
(
set -x
$SUDO dmesg -n 8
$SUDO modprobe netconsole "netconsole=6666@$SRC_IP/$SRC_DEV,$TGT_PORT@$TGT_IP/$TGT_MAC"
)
echo DONE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment