Skip to content

Instantly share code, notes, and snippets.

@jigpu
Last active December 1, 2016 19:53
Show Gist options
  • Save jigpu/dc53bc5da68977b28d15eca083d33c30 to your computer and use it in GitHub Desktop.
Save jigpu/dc53bc5da68977b28d15eca083d33c30 to your computer and use it in GitHub Desktop.
Script to easily enable netconsole and change kernel verbosity
#!/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
DEV=""
HOST=""
LEVEL=7
function HELP {
echo "Usage: $(basename $0) [@dev] <ip address> [verbosity]"
echo
echo "Accepted numeric verbosity levels are:"
for i in "${!VERBOSITY[@]}"; do
echo " $i (${VERBOSITY[$i]})"
done
}
function ERROR {
echo $1
HELP
exit $2
}
function ERRORMISSINGARGS {
ERROR "Missing required arguments." 1
}
if [[ $# -eq 0 ]]; then ERRORMISSINGARGS; fi
if [[ $1 == "@"* ]]; then DEV=${1:1}; shift; fi
if [[ $# -eq 0 ]]; then ERRORMISSINGARGS; fi
HOST=$1
IP=$(getent ahostsv4 "$HOST" | grep STREAM | cut -f1 -d' ')
shift
if [[ $# -gt 0 ]]; then LEVEL=$1; shift; fi
if [[ $# -gt 0 ]]; then
ERROR "Too many arguments encountered." 2
fi
if [[ ! " ${!VERBOSITY[@]} " =~ " $LEVEL " ]]; then
ERROR "Unknown verbosity level." 3
fi
if [[ "$DEV" == "" ]]; then
DEV=$(ip route show to match "$IP" | grep -o "dev .*" | cut -f2 -d' ' | head -n1)
fi
echo "Please run \`netcat -u -l 6666\` on the target machine at $IP now."
echo "Press <Enter> when ready..."
read
OLDLEVEL=$(cut -f1 /proc/sys/kernel/printk)
echo "Increasing console verbosity to from $OLDLEVEL (${VERBOSITY[$OLDLEVEL]}) to $LEVEL (${VERBOSITY[$LEVEL]})"
echo $LEVEL > /proc/sys/kernel/printk
echo "Sending console output via UDP from interface ${DEV} to $IP:6666"
modprobe -r netconsole
modprobe netconsole netconsole=@/${DEV},@${IP}/
echo
echo "You should now see logs appearing on the target machine. If not,"
echo "please verify the target machine's IP address is ${IP} and"
echo "that no firewalls are blocking traffic."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment