Skip to content
Snippets Groups Projects
Commit a3348ec3 authored by Jens Glathe's avatar Jens Glathe Committed by Jens Glathe
Browse files

add options --bluetooth-if and --wlan-if for giving the device name to the script


add mandatory parameter checking for right format of if-name and bytes

Signed-off-by: default avatarJens Glathe <jens.glathe@oldschoolsolutions.biz>
parent 2b6e9176
No related branches found
Tags v0.5.0
1 merge request!7add parameter for variable prefix, try to down hci0 before accessing it, add parameters for interface names
Pipeline #141384 passed
......@@ -23,93 +23,88 @@ log() {
help_info() {
echo "bootmac options:"
echo " -b | --bluetooth = Configure Bluetooth interface's MAC"
echo " -w | --wlan = Configure WLAN interface's MAC"
echo " -g | --generate-only = Generate and print the MAC address to stdout, but do not configure the interface. This option must be specified with either -b or -w, but not both."
echo " -p | --prefix <2 bytes> = Set a prefix other than 0200"
echo " -b | --bluetooth = Configure Bluetooth interface's MAC"
echo " -B | --bluetooth-if <if-name> = Specify the Bluetooth interface name, like hci0. Implies -b"
echo " -w | --wlan = Configure WLAN interface's MAC"
echo " -W | --wlan-if <if-name> = Specify the WLAN interface name, like wlP6p1s0. Implies -w"
echo " -g | --generate-only = Generate and print the MAC address to stdout, but do not configure the interface."
echo " This option must be specified with either -b or -w, but not both."
echo " -p | --prefix <bytes> = Set a prefix other than 0200, can be longer than 2 bytes, like IEEE OUI"
echo ""
echo "bootmac will configure the MAC addresses for both interfaces if no arguments are passed"
}
unknown_option() {
echo "ERROR: Unknown argument detected." >&2
echo "" >&2
help_info
exit 1
}
# Set default values for arguments
WLAN_CONFIGURE_ARG=0
BLUETOOTH_CONFIGURE_ARG=0
GENERATE_ONLY_ARG=0
# Helper function to validate interface name
validate_interface_name() {
if ! echo "$1" | grep -Eq '^[a-zA-Z0-9]+$'; then
echo "ERROR: Invalid interface name '$1'. Interface names should only contain alphanumeric characters." >&2
exit 1
fi
}
# Helper function to validate hexadecimal string
validate_hex_string() {
if ! echo "$1" | grep -Eq '^[0-9a-fA-F]{4,}$'; then
echo "ERROR: Invalid hexadecimal string '$1'. It should be at least 2 octets long with characters in the range 0-9, a-f, and no separators." >&2
exit 1
fi
}
# If no arguments are passed, default to configuring both WLAN and Bluetooth
if [ "$#" -eq 0 ]; then
WLAN_CONFIGURE_ARG=1
BLUETOOTH_CONFIGURE_ARG=1
fi
# Use the "getopts" built-in to parse arguments
while getopts bwhgp-: arg; do
case "$arg" in
w)
# Parse arguments if any are passed
while [ "$#" -gt 0 ]; do
case "$1" in
-w|--wlan)
WLAN_CONFIGURE_ARG=1
shift
;;
-b|--bluetooth)
BLUETOOTH_CONFIGURE_ARG=1
shift
;;
-W|--wlan-if)
WLAN_CONFIGURE_ARG=1
validate_interface_name "$2"
WLAN_INTERFACE="$2"
shift 2
;;
b)
-B|--bluetooth-if)
BLUETOOTH_CONFIGURE_ARG=1
validate_interface_name "$2"
BT_INTERFACE="$2"
shift 2
;;
g)
-p|--prefix)
validate_hex_string "$2"
MAC_PREFIX="$2"
shift 2
;;
-g|--generate-only)
GENERATE_ONLY_ARG=1
shift
;;
h)
-h|--help)
help_info
exit 0
;;
p)
if [[ -z "$OPTARG" || "$OPTARG" == -* ]]; then
echo "Error: Option -p requires an argument."
exit 1
fi
MAC_PREFIX="$OPTARG"
;;
-)
case "$OPTARG" in
wlan)
WLAN_CONFIGURE_ARG=1
;;
bluetooth)
BLUETOOTH_CONFIGURE_ARG=1
;;
generate-only)
GENERATE_ONLY_ARG=1
;;
prefix)
if [[ -z "$OPTARG" || "$OPTARG" == -* ]]; then
echo "Error: Option -p requires an argument."
exit 1
fi
MAC_PREFIX="$OPTARG"
;;
help)
help_info
exit 0
;;
*)
unknown_option
;;
esac
;;
*)
unknown_option
*) # If unknown argument, display help
echo "ERROR: Unknown argument detected: $1" >&2
help_info
exit 1
;;
esac
done
# If any command-line options remained unparsed, they're unknown
if [ "$((OPTIND-1))" != "$#" ]; then
unknown_option
fi
# If "generate-only" is specified, either Wi-Fi or Bluetooth must be requested, but not both
if [ "$GENERATE_ONLY_ARG" = 1 ] && [ "$((WLAN_CONFIGURE_ARG+BLUETOOTH_CONFIGURE_ARG))" != 1 ]; then
echo "ERROR: Either Wi-Fi or Bluetooth must be requested together with --generate-only." >&2
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment