Last active
January 20, 2016 18:33
-
-
Save joestringer/aef8c25485e7af1988fa to your computer and use it in GitHub Desktop.
Get netconsole options to send logs to a given IP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# gnco.sh: Get netconsole options to send logs to a given IP. | |
# Author: Joe Stringer | |
# | |
# v1.1: Minor script tidyups. | |
# v1.0: Initial post. | |
# $1: L3 destination | |
function get_l2() | |
{ | |
ping -c 1 $1 > /dev/null | |
arp -n $1 | tail -n 1 | sed 's/ */ /g' | cut -d' ' -f3 | |
} | |
# $1: Output of 'ip route get foo' | |
# $2: Parameter to search for, eg "via", "dev" | |
function get_route_param() | |
{ | |
EXPR="s/.*\($2 [^ ]*\)/\1/" | |
echo $1 | sed -e "$EXPR" -e 's/ */ /' | cut -d' ' -f2 | |
} | |
# $1: MAC address | |
function is_mac_valid() | |
{ | |
echo $1 | grep ":" 2>&1>/dev/null | |
} | |
function main() | |
{ | |
if [ $# -lt 1 ] || [ $# -gt 1 ]; then | |
echo "Usage: $0 <dst-ip>" | |
exit 1; | |
fi | |
L2_DST="???" | |
L3_DST=$1 | |
L4_SRC=6666 | |
L4_DST=6666 | |
GATEWAY=$L3_DST | |
ROUTE=`ip route get $L3_DST` | |
IFACE=`get_route_param "$ROUTE" "dev"` | |
L3_SRC=`get_route_param "$ROUTE" "src"` | |
if ! echo "$ROUTE" | grep "via" 2>&1>/dev/null; then | |
GATEWAY=`get_route_param $ROUTE "via"` | |
fi | |
L2_DST=`get_l2 $GATEWAY` | |
if ! is_mac_valid $L2_DST; then | |
# Fall back to default route | |
GATEWAY=`netstat -rn | grep ^0.0.0.0 | sed 's/ */ /' | cut -d' ' -f2` | |
L2_DST=`get_l2 $GATEWAY` | |
if ! is_mac_valid $L2_DST; then | |
echo "Couldn't resolve gateway '$GATEWAY'" 1>&2 | |
exit 1 | |
fi | |
fi | |
echo netconsole=${L4_SRC}@${L3_SRC}/${IFACE},${L4_DST}@${L3_DST}/${L2_DST} | |
} | |
main $* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment