Skip to content

Instantly share code, notes, and snippets.

@jigpu
Created January 15, 2016 00:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jigpu/948f7bc6202f45ea9d42 to your computer and use it in GitHub Desktop.
Save jigpu/948f7bc6202f45ea9d42 to your computer and use it in GitHub Desktop.
Setup the netconsole module to send kernel logs to a remote machine
#!/usr/bin/env bash
########
# Script to easily enable netconsole and change kernel verbosity
#
# See http://elinux.org/Debugging_py_printing
#
set -e
declare -A VERBOSITY
VERBOSITY[0]=KERN_EMERG
VERBOSITY[1]=KERN_ALERT
VERBOSITY[2]=KERN_CRIT
VERBOSITY[3]=KERN_ERR
VERBOSITY[4]=KERN_WARNING
VERBOSITY[5]=KERN_NOTICE
VERBOSITY[6]=KERN_INFO
VERBOSITY[7]=KERN_DEBUG
LEVEL=7
ERR=""
if [[ $# -eq 0 ]]; then ERR="Missing required arguments."; fi
if [[ $# -ge 1 ]]; then IP=$1; fi
if [[ $# -ge 2 ]]; then LEVEL=$2; fi
if [[ $# -ge 3 ]]; then ERR="Too many arguments."; fi
if [[ ! " ${!VERBOSITY[@]} " =~ " $LEVEL " ]]; then
ERR="Unknown verbosity level."
fi
if [[ "x$ERR" != "x" ]]; then
echo "Error: $ERR"
echo "Usage: $(basename $0) <ip address> [verbosity]"
echo
echo "Accepted numeric verbosity levels are:"
for i in "${!VERBOSITY[@]}"; do
echo " $i (${VERBOSITY[$i]})"
done
exit 1
fi
echo "Sending console output via UDP to $IP:6666"
modprobe netconsole netconsole=@/,@${IP}/
echo "Increasing console verbosity to $LEVEL (${VERBOSITY[$LEVEL]})"
echo $LEVEL > /proc/sys/kernel/printk
echo "Please run \`netcat -u -l 6666\` on the target machine at $IP now."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment