2017-08-17 16:37:44 -06:00
|
|
|
#!/bin/bash
|
|
|
|
|
2017-08-31 14:08:43 -06:00
|
|
|
#if [ "$InterfaceUtilsVersion" ]; then return 0; fi
|
|
|
|
#readonly InterfaceUtilsVersion="1.0"
|
2017-08-22 13:23:08 -06:00
|
|
|
|
2017-08-17 16:37:44 -06:00
|
|
|
# The methods used in this script are taken from airmon-ng.
|
|
|
|
# This is all thanks for the airmon-ng authors, thanks guys.
|
2017-08-17 22:11:21 -06:00
|
|
|
InterfaceUtilsOutputDevice="/dev/stdout"
|
|
|
|
|
|
|
|
if [ -d /sys/bus/usb ] # && hash lsusb
|
|
|
|
then InterfaceUSBBus=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d /sys/bus/pci ] || [ -d /sys/bus/pci_express ] || [ -d /proc/bus/pci ] # && hash lspci
|
|
|
|
then InterfacePCIBus=1
|
|
|
|
fi
|
|
|
|
|
2017-08-17 16:37:44 -06:00
|
|
|
function interface_list_all() {
|
|
|
|
InterfaceListAll=($(ls -1 /sys/class/net))
|
|
|
|
}
|
|
|
|
|
|
|
|
function interface_list_wireless() {
|
|
|
|
InterfaceListWireless=()
|
|
|
|
interface_list_all
|
|
|
|
local __interface_list_wireless__candidate
|
|
|
|
for __interface_list_wireless__candidate in "${InterfaceListAll[@]}"; do
|
2017-08-30 14:55:16 -06:00
|
|
|
if interface_wireless $__interface_list_wireless__candidate
|
2017-08-17 16:37:44 -06:00
|
|
|
then InterfaceListWireless+=("$__interface_list_wireless__candidate")
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
2017-08-17 17:01:17 -06:00
|
|
|
|
2017-08-30 14:55:16 -06:00
|
|
|
function interface_wireless() {
|
|
|
|
if grep -qs "DEVTYPE=wlan" /sys/class/net/$1/uevent
|
|
|
|
then return 0
|
|
|
|
else return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-08-17 17:01:17 -06:00
|
|
|
function interface_driver() {
|
|
|
|
InterfaceDriver=$(basename $(readlink /sys/class/net/$1/device/driver))
|
|
|
|
}
|
|
|
|
|
2017-08-17 22:11:21 -06:00
|
|
|
function interface_physical() {
|
|
|
|
if [ ! "$1" ]; then return 1; fi
|
|
|
|
|
|
|
|
unset InterfacePhysical
|
|
|
|
|
|
|
|
local -r interface_physical_path="/sys/class/net/$1/phy80211"
|
|
|
|
|
|
|
|
if [ -d "$interface_physical_path" ]; then
|
|
|
|
if [ -r "$interface_physical_path/name" ]
|
|
|
|
then InterfacePhysical="`cat "$interface_physical_path/name"`"
|
2017-08-20 14:08:42 -06:00
|
|
|
fi
|
2017-08-20 14:19:40 -06:00
|
|
|
if [ ! "${InterfacePhysical// }" ]
|
|
|
|
then InterfacePhysical="`ls -l "$interface_physical_path" | sed 's/^.*\/\([a-zA-Z0-9_-]*\)$/\1/'`"
|
2017-08-17 22:11:21 -06:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! "$InterfacePhysical" ]; then return 2; fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function interface_hardware() {
|
|
|
|
if [ ! "$1" ]; then return 1; fi
|
|
|
|
|
|
|
|
local __interface_hardware__device="/sys/class/net/$1/device"
|
|
|
|
local __interface_hardware__hwinfo="$__interface_hardware__device/modalias"
|
|
|
|
|
2017-08-17 22:24:29 -06:00
|
|
|
InterfaceHardwareBus="`cut -d ":" -f 1 "$__interface_hardware__hwinfo" 2> $InterfaceUtilsOutputDevice`"
|
2017-08-17 22:11:21 -06:00
|
|
|
|
|
|
|
case "$InterfaceHardwareBus" in
|
|
|
|
"usb") # Wanted to replace the line below with awk, but i'll probably just add complexity & issues (mawk vs gawk).
|
2017-08-17 22:24:29 -06:00
|
|
|
InterfaceHardwareID="`cut -d ":" -f 2 $__interface_hardware__hwinfo | cut -b 1-10 | sed 's/^.//;s/p/:/'`";;
|
2017-08-17 22:11:21 -06:00
|
|
|
"pci" | "pcmcia" | "sdio")
|
|
|
|
InterfaceHardwareID="`cat "$__interface_hardware__device/vendor" 2> $InterfaceUtilsOutputDevice`:`cat "$__interface_hardware__device/device" 2> $InterfaceUtilsOutputDevice`";;
|
|
|
|
default) # The following will only work for USB devices.
|
2017-08-17 22:24:29 -06:00
|
|
|
InterfaceHardwareID="`cat "$__interface_hardware__device/idVendor" 2> $InterfaceUtilsOutputDevice`:`cat "$__interface_hardware__device/idProduct" 2> $InterfaceUtilsOutputDevice`"
|
|
|
|
InterfaceHardwareBus="usb";; # This will be reset below if InterfaceHardwareID is invalid.
|
2017-08-17 22:11:21 -06:00
|
|
|
esac
|
|
|
|
|
|
|
|
# Check for invalid InterfaceHardwareID (starts or ends with :) .. not a happy face, still won't quote it.
|
|
|
|
if echo "$InterfaceHardwareID" | egrep -q "^:|:$"; then
|
|
|
|
unset InterfaceHardwareID
|
|
|
|
unset InterfaceHardwareBus
|
|
|
|
return 2
|
2017-08-30 14:55:16 -06:00
|
|
|
else
|
|
|
|
# Remove any extraneous hex markers.
|
|
|
|
InterfaceHardwareID=${InterfaceHardwareID//0x/}
|
2017-08-17 22:11:21 -06:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-08-17 17:01:17 -06:00
|
|
|
function interface_chipset() {
|
2017-08-17 22:11:21 -06:00
|
|
|
if [ ! "$1" ]; then return 1; fi
|
|
|
|
|
|
|
|
if ! interface_hardware "$1"; then return 2; fi
|
|
|
|
|
|
|
|
case "$InterfaceHardwareBus" in
|
|
|
|
"usb")
|
|
|
|
if [ ! "$InterfaceUSBBus" ]; then return 3; fi
|
2017-08-17 22:24:29 -06:00
|
|
|
InterfaceChipset="`lsusb -d "$InterfaceHardwareID" | head -n1 - | cut -f3- -d ":" | sed 's/^....//;s/ Network Connection//g;s/ Wireless Adapter//g;s/^ //'`";;
|
2017-08-17 22:11:21 -06:00
|
|
|
"pci" | "pcmcia")
|
|
|
|
if [ ! "$InterfacePCIBus" ]; then return 4; fi
|
2017-08-17 22:24:29 -06:00
|
|
|
InterfaceChipset="$(lspci -d $InterfaceHardwareID | cut -f3- -d ":" | sed 's/Wireless LAN Controller //g;s/ Network Connection//g;s/ Wireless Adapter//;s/^ //')";;
|
2017-08-17 22:11:21 -06:00
|
|
|
"sdio")
|
|
|
|
if [[ "${InterfaceHardwareID,,}" = "0x02d0"* ]]
|
|
|
|
then InterfaceChipset=$(printf "Broadcom %d" ${InterfaceHardwareID:7})
|
|
|
|
else InterfaceChipset="Unknown chipset for SDIO device."
|
|
|
|
fi;;
|
|
|
|
default)
|
|
|
|
InterfaceChipset="Unknown device chipset & device bus."
|
|
|
|
esac
|
2017-08-17 17:01:17 -06:00
|
|
|
}
|
2017-08-17 22:11:21 -06:00
|
|
|
|
2017-08-18 00:52:51 -06:00
|
|
|
function interface_state() {
|
|
|
|
if [ ! "$1" ]; then return 1; fi
|
|
|
|
local __interface_state__stateFile="/sys/class/net/$1/operstate"
|
|
|
|
|
|
|
|
if [ ! -f "$__interface_state__stateFile" ]; then return 2; fi
|
|
|
|
InterfaceState=$(cat "$__interface_state__stateFile")
|
|
|
|
}
|
|
|
|
|
|
|
|
function interface_set_state() {
|
|
|
|
if [ "${#@}" -ne 2 ]; then return 1; fi
|
|
|
|
ip link set "$1" "$2"
|
|
|
|
}
|
|
|
|
|
|
|
|
function interface_set_mode() {
|
|
|
|
if [ "${#@}" -ne 2 ]; then return 1; fi
|
|
|
|
if ! interface_set_state "$1" "down"; then return 2; fi
|
|
|
|
if ! iwconfig "$1" mode "$2" &> $InterfaceUtilsOutputDevice; then return 3; fi
|
|
|
|
if ! interface_set_state "$1" "up"; then return 4; fi
|
|
|
|
}
|
2017-08-31 14:08:43 -06:00
|
|
|
|
|
|
|
function interface_prompt() {
|
|
|
|
if [ -z "$1" -o -z "$2" ]; then return 1; fi
|
|
|
|
|
|
|
|
local __interface_prompt__ifAvailable=()
|
|
|
|
local __interface_prompt__ifAvailableInfo=()
|
|
|
|
local __interface_prompt__ifAvailableColor=()
|
|
|
|
local __interface_prompt__ifAvailableState=()
|
|
|
|
|
|
|
|
local __interface_prompt__ifCandidate
|
|
|
|
for __interface_prompt__ifCandidate in "${!2}"; do
|
|
|
|
# Skip all non-device interfaces, such as the loopback interface.
|
|
|
|
# Notice: The conditional will NOT skip all virutal interfaces.
|
|
|
|
if [ ! -d /sys/class/net/$__interface_prompt__ifCandidate/device ]
|
|
|
|
then continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
__interface_prompt__ifAvailable+=("$__interface_prompt__ifCandidate")
|
|
|
|
|
|
|
|
interface_chipset "$__interface_prompt__ifCandidate"
|
|
|
|
__interface_prompt__ifAvailableInfo+=("$InterfaceChipset")
|
|
|
|
|
|
|
|
interface_state "$__interface_prompt__ifCandidate"
|
|
|
|
|
|
|
|
if [ "$InterfaceState" = "up" ]
|
|
|
|
then __interface_prompt__ifAvailableColor+=("$CPrp"); __interface_prompt__ifAvailableState+=("[-]")
|
|
|
|
else __interface_prompt__ifAvailableColor+=("$CClr"); __interface_prompt__ifAvailableState+=("[+]")
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# The following conditional is required since io_query_format_fields
|
|
|
|
# only considers the the size of the first parameter, available color.
|
|
|
|
if [ "$6" ]; then # Add alternative choices
|
|
|
|
__interface_prompt__ifAvailable+=("${!3}")
|
|
|
|
__interface_prompt__ifAvailableInfo+=("${!4}")
|
|
|
|
__interface_prompt__ifAvailableState+=("${!5}")
|
|
|
|
__interface_prompt__ifAvailableColor+=("${!6}")
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If only one interface exists and it's available, choose it.
|
|
|
|
if [ ${#__interface_prompt__ifAvailable[@]} -eq 1 -a ${__interface_prompt__ifAvailableState[0]} = "[+]" ]; then
|
|
|
|
InterfacePromptWISelected="${__interface_prompt__ifAvailable[0]}"
|
|
|
|
InterfacePromptWISelectedState="[+]" # It passed the condition, it must be +
|
|
|
|
else
|
|
|
|
format_apply_autosize "$CRed[$CYel%1d$CRed]%b %-8b %3s %-*.*s\n"
|
|
|
|
io_query_format_fields "$1" "$FormatApplyAutosize" \
|
|
|
|
__interface_prompt__ifAvailableColor[@] __interface_prompt__ifAvailable[@] \
|
|
|
|
__interface_prompt__ifAvailableState[@] __interface_prompt__ifAvailableInfo[@]
|
|
|
|
|
|
|
|
echo
|
|
|
|
|
|
|
|
InterfacePromptIfSelected="${IOQueryFormatFields[1]}"
|
|
|
|
InterfacePromptIfSelectedState="${IOQueryFormatFields[2]}"
|
|
|
|
fi
|
|
|
|
}
|