Last active
September 17, 2017 11:34
-
-
Save JadedDragoon/74b31db715d974aaea883177bb9757c7 to your computer and use it in GitHub Desktop.
A requested script for setting jumbo frames on thousands of unknowable interfaces
This file contains hidden or 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 | |
#=============================================================================== | |
# MIT License | |
# | |
# Copyright (c) 2017 Jeremy Cliff Armstrong | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
#=============================================================================== | |
#_opts=`getopt -o t:ab:m:p:h --long threaded:,altbin,bin:,mtu:,prefix:,help -- "$@"` | |
_opts=`getopt -o ab:m:p:s:h --long altbin,bin:,mtu:,prefix:,help -- "$@"` | |
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi | |
#echo "$_opts" | |
eval set -- "$_opts" | |
_help=0 | |
while true | |
do | |
case "$1" in | |
#-t | --threaded ) _THREADS="$2"; shift 2 ;; | |
-b | --bin ) _BIN="$2"; shift 2 ;; | |
-m | --mtu ) _MTU="$2"; shift 2 ;; | |
-p | --prefix ) _PRE="$2"; shift 2 ;; | |
-h | --altbin ) _ALTBIN=1; shift ;; | |
-h | --help ) _help=1; shift ;; | |
-- ) shift; break ;; | |
* ) break ;; | |
esac | |
done | |
if [[ _help -gt 0 ]]; then | |
# 0 1 2 3 4 5 6 7 8 | |
# |123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789| | |
echo "vi-jumboframe.sh: Usage" | |
echo | |
echo "Sets MTU to 9000 on all interfaces beginning with -p/--prefix and found in" | |
echo "'/sys/class/net/'." | |
echo | |
#echo " -t|--threaded Enabled threaded mode. Requires the parallel utility. You" | |
#echo " must specify the number of threads to use (default=off)" | |
#echo | |
echo " -a|--altbin This script normally uses the ifconfig binary to set MTU. On" | |
echo " systems where ifconfig is not available this flag will" | |
echo " enable using the ip binary instead." | |
echo | |
echo " -b|--bin The full path to the executable ifconfig binary. Defaults to" | |
echo " 'ifconfig' and relies on the path to find the binary. If" | |
echo " used with -a/--altbin sets the full path for the ip binary." | |
echo | |
echo " -m|--mtu The MTU to set each interface to. Defaults to 9000." | |
echo | |
echo " -p|--prefix Limits affected interfaces to those beginning with provided" | |
echo " prefix. Defaults to 'kvm'." | |
echo | |
echo " -h|--help Display this message and exit." | |
echo | |
echo "------------------------------------------------------------------------------" | |
echo | |
echo "Examples:" | |
echo | |
echo " unthreaded, use alternate binary, binary located at '/usr/sbin/ip'" | |
echo | |
echo " vi-jumboframe.sh -a -b /usr/sbin/ip" | |
echo | |
#echo " 4 threads, interface prefix 'virt'" | |
#echo | |
#echo " vi-jumboframe.sh -t 4 -p virt" | |
#echo | |
#echo " 16 threads, binary located at '/sbin/ifconfig'" | |
#echo | |
#echo " vi-jumboframe.sh -t 16 -b /usr/sbin/ifconfig" | |
#echo | |
echo "Exit Codes:" | |
echo | |
echo " 0: Success." | |
echo " 1: Failed - no changes were made." | |
echo " 2: Failed - Changes may have been made, potentially inconsistent state." | |
echo | |
exit 0 | |
fi | |
if [ -z ${_ALTBIN:+x} ]; then _ALTBIN=0; fi | |
#if [ -z ${_THREADS:+x} ]; then _THREADS=0; fi | |
if [ -z ${_MTU:+x} ]; then _MTU=9000; fi | |
if [ -z ${_PRE:+x} ]; then _PRE=kvm; fi | |
if [ -z ${_BIN:+x} ]; then | |
if [[ ${_ALTBIN} -le 0 ]]; then _BIN=ifconfig; else _BIN=ip; fi | |
fi | |
if ! [[ ${_MTU} =~ ^[0-9]+$ ]]; then echo "$0: The MTU must be given as an integer." >&2; exit 1; fi | |
#if ! [[ ${_THREADS} =~ ^[0-9]+$ ]]; then echo "$0: The number of threads must be given as an integer." >&2; exit 1; fi | |
cd /sys/class/net | |
shopt -s nullglob | |
__devlist=( ${_PRE}* ) | |
if [[ ${_ALTBIN} -le 0 ]] | |
then | |
#if [[ ${_THREADS} -le 0 ]] | |
#then | |
# Using ifconfig, non-threaded | |
for __dev in ${__devlist[@]} | |
do | |
if ! ${_BIN} ${__dev} mtu ${_MTU} >/dev/null; then echo "$0: Failed to set MTU on interface '${__dev}'. Exiting early." >&2; exit 2; fi | |
done | |
#else | |
# # Using ifconfig, threaded (not yet implimented) | |
#fi | |
else | |
#if [[ ${_THREADS} -le 0 ]] | |
#then | |
# Using ip, non-threaded | |
for __dev in ${__devlist[@]} | |
do | |
if ! ${_BIN} link set mtu ${_MTU} dev ${__dev} >/dev/null; then echo "$0: Failed to set MTU on interface '${__dev}'. Exiting early." >&2; exit 2; fi | |
done | |
#else | |
# # Using ip, threaded (not yet implimented) | |
#fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment