2018-05-02 12:50:31 -06:00
|
|
|
#!/usr/bin/env bash
|
2017-08-17 16:37:44 -06:00
|
|
|
|
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"
|
|
|
|
|
2017-12-31 09:02:12 -07:00
|
|
|
if [ -d /sys/bus/usb ] # && hash lsusb;
|
|
|
|
then InterfaceUSBBus=1
|
2017-08-17 22:11:21 -06:00
|
|
|
fi
|
|
|
|
|
2018-01-05 17:32:57 -07:00
|
|
|
if [ -d /sys/bus/pci -o -d /sys/bus/pci_express -o -d /proc/bus/pci ] # && hash lspci;
|
2017-08-31 16:31:05 -06:00
|
|
|
then InterfacePCIBus=1
|
2017-08-17 22:11:21 -06:00
|
|
|
fi
|
|
|
|
|
2017-08-31 16:31:05 -06:00
|
|
|
# Checks if the interface belongs to a physical device.
|
|
|
|
function interface_is_real() {
|
2018-01-05 17:32:57 -07:00
|
|
|
test -d /sys/class/net/$1/device
|
|
|
|
return $?
|
2017-08-31 16:31:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# Checks if the interface belongs to a wireless device.
|
|
|
|
function interface_is_wireless() {
|
2018-01-05 17:32:57 -07:00
|
|
|
grep -qs "DEVTYPE=wlan" /sys/class/net/$1/uevent
|
|
|
|
return $?
|
2017-08-31 16:31:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# Returns an array of absolutely all interfaces.
|
|
|
|
# Notice: That includes interfaces such as the loopback interface.
|
2017-08-17 16:37:44 -06:00
|
|
|
function interface_list_all() {
|
2017-12-31 09:02:12 -07:00
|
|
|
InterfaceListAll=(/sys/class/net/*)
|
|
|
|
InterfaceListAll=("${InterfaceListAll[@]//\/sys\/class\/net\//}")
|
2017-08-17 16:37:44 -06:00
|
|
|
}
|
|
|
|
|
2017-08-31 16:31:05 -06:00
|
|
|
# Returns an array of interfaces pertaining to a physical device.
|
|
|
|
function interface_list_real() {
|
2017-12-31 09:02:12 -07:00
|
|
|
InterfaceListReal=()
|
|
|
|
interface_list_all
|
|
|
|
local __interface_list_real__candidate
|
|
|
|
for __interface_list_real__candidate in "${InterfaceListAll[@]}"; do
|
|
|
|
if interface_is_real $__interface_list_real__candidate; then InterfaceListReal+=("$__interface_list_real__candidate")
|
|
|
|
fi
|
|
|
|
done
|
2017-08-31 16:31:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# Returns an array of interfaces pertaining to a wireless device.
|
2017-08-17 16:37:44 -06:00
|
|
|
function interface_list_wireless() {
|
2017-12-31 09:02:12 -07:00
|
|
|
InterfaceListWireless=()
|
|
|
|
interface_list_all
|
|
|
|
local __interface_list_wireless__candidate
|
|
|
|
for __interface_list_wireless__candidate in "${InterfaceListAll[@]}"; do
|
|
|
|
if interface_is_wireless $__interface_list_wireless__candidate; then InterfaceListWireless+=("$__interface_list_wireless__candidate")
|
|
|
|
fi
|
|
|
|
done
|
2017-08-17 16:37:44 -06:00
|
|
|
}
|
2017-08-17 17:01:17 -06:00
|
|
|
|
|
|
|
function interface_driver() {
|
2017-12-31 09:02:12 -07:00
|
|
|
InterfaceDriver=$(basename $(readlink /sys/class/net/$1/device/driver))
|
2017-08-17 17:01:17 -06:00
|
|
|
}
|
|
|
|
|
2017-08-17 22:11:21 -06:00
|
|
|
function interface_physical() {
|
2017-12-31 09:02:12 -07:00
|
|
|
if [ ! "$1" ]; then return 1; fi
|
2017-08-17 22:11:21 -06:00
|
|
|
|
2017-12-31 09:02:12 -07:00
|
|
|
unset InterfacePhysical
|
2017-08-17 22:11:21 -06:00
|
|
|
|
2017-12-31 09:02:12 -07:00
|
|
|
local -r interface_physical_path="/sys/class/net/$1/phy80211"
|
2017-08-17 22:11:21 -06:00
|
|
|
|
2017-12-31 09:02:12 -07:00
|
|
|
if [ -d "$interface_physical_path" ]; then
|
|
|
|
if [ -r "$interface_physical_path/name" ]; then InterfacePhysical="$(cat "$interface_physical_path/name")"
|
|
|
|
fi
|
|
|
|
if [ ! "${InterfacePhysical// /}" ]; then InterfacePhysical="$(ls -l "$interface_physical_path" | sed 's/^.*\/\([a-zA-Z0-9_-]*\)$/\1/')"
|
|
|
|
fi
|
|
|
|
fi
|
2017-08-17 22:11:21 -06:00
|
|
|
|
2017-12-31 09:02:12 -07:00
|
|
|
if [ ! "$InterfacePhysical" ]; then return 2; fi
|
2017-08-17 22:11:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function interface_hardware() {
|
2017-12-31 09:02:12 -07:00
|
|
|
if [ ! "$1" ]; then return 1; fi
|
|
|
|
|
|
|
|
local __interface_hardware__device="/sys/class/net/$1/device"
|
|
|
|
local __interface_hardware__hwinfo="$__interface_hardware__device/modalias"
|
|
|
|
|
|
|
|
InterfaceHardwareBus="$(cut -d ":" -f 1 "$__interface_hardware__hwinfo" 2>$InterfaceUtilsOutputDevice)"
|
|
|
|
|
|
|
|
case "$InterfaceHardwareBus" in
|
|
|
|
"usb") # Wanted to replace the line below with awk, but i'll probably just add complexity & issues (mawk vs gawk).
|
|
|
|
InterfaceHardwareID="$(cut -d ":" -f 2 $__interface_hardware__hwinfo | cut -b 1-10 | sed 's/^.//;s/p/:/')"
|
|
|
|
;;
|
|
|
|
"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.
|
|
|
|
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.
|
|
|
|
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
|
|
|
|
else
|
|
|
|
# Remove any extraneous hex markers.
|
|
|
|
InterfaceHardwareID=${InterfaceHardwareID//0x/}
|
|
|
|
fi
|
2017-08-17 22:11:21 -06:00
|
|
|
}
|
|
|
|
|
2017-08-17 17:01:17 -06:00
|
|
|
function interface_chipset() {
|
2017-12-31 09:02:12 -07: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
|
|
|
|
InterfaceChipset="$(lsusb -d "$InterfaceHardwareID" | head -n1 - | cut -f3- -d ":" | sed 's/^....//;s/ Network Connection//g;s/ Wireless Adapter//g;s/^ //')"
|
|
|
|
;;
|
|
|
|
"pci" | "pcmcia")
|
|
|
|
if [ ! "$InterfacePCIBus" ]; then return 4; fi
|
|
|
|
InterfaceChipset="$(lspci -d $InterfaceHardwareID | cut -f3- -d ":" | sed 's/Wireless LAN Controller //g;s/ Network Connection//g;s/ Wireless Adapter//;s/^ //')"
|
|
|
|
;;
|
|
|
|
"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() {
|
2017-12-31 09:02:12 -07:00
|
|
|
if [ ! "$1" ]; then return 1; fi
|
|
|
|
local __interface_state__stateFile="/sys/class/net/$1/operstate"
|
2017-08-18 00:52:51 -06:00
|
|
|
|
2017-12-31 09:02:12 -07:00
|
|
|
if [ ! -f "$__interface_state__stateFile" ]; then return 2; fi
|
|
|
|
InterfaceState=$(cat "$__interface_state__stateFile")
|
2017-08-18 00:52:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function interface_set_state() {
|
2017-12-31 09:02:12 -07:00
|
|
|
if [ "${#@}" -ne 2 ]; then return 1; fi
|
2018-01-05 17:32:57 -07:00
|
|
|
# TODO: Add alternatives to 'ip' in case of failure.
|
2017-12-31 09:02:12 -07:00
|
|
|
ip link set "$1" "$2"
|
2018-01-05 17:32:57 -07:00
|
|
|
return $?
|
2017-08-18 00:52:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function interface_set_mode() {
|
2017-12-31 09:02:12 -07:00
|
|
|
if [ "${#@}" -ne 2 ]; then return 1; fi
|
|
|
|
if ! interface_set_state "$1" "down"; then return 2; fi
|
2018-01-05 17:32:57 -07:00
|
|
|
if ! iw dev "$1" set type "$2" &> $InterfaceUtilsOutputDevice; then
|
|
|
|
if ! iwconfig "$1" mode "$2" &> $InterfaceUtilsOutputDevice
|
|
|
|
then return 3
|
|
|
|
fi
|
|
|
|
fi
|
2017-12-31 09:02:12 -07:00
|
|
|
if ! interface_set_state "$1" "up"; then return 4; fi
|
2017-08-18 00:52:51 -06:00
|
|
|
}
|
2017-08-31 14:08:43 -06:00
|
|
|
|
2018-01-05 17:32:57 -07:00
|
|
|
function interface_reidentify() {
|
|
|
|
if [ ${#@} -ne 2 ]; then return 1; fi
|
2018-05-29 16:38:10 -06:00
|
|
|
|
2018-01-05 17:32:57 -07:00
|
|
|
local -r __interface_reidentify__oldIdentifier=$1
|
|
|
|
local -r __interface_reidentify__newIdentifier=$2
|
2018-05-29 16:38:10 -06:00
|
|
|
|
2018-01-05 17:32:57 -07:00
|
|
|
if [[ $__interface_reidentify__newIdentifier == *" "* ]]
|
|
|
|
then return 2
|
|
|
|
fi
|
2018-05-29 16:38:10 -06:00
|
|
|
|
2018-01-05 17:32:57 -07:00
|
|
|
if ! interface_set_state $__interface_reidentify__oldIdentifier down
|
|
|
|
then return 3
|
|
|
|
fi
|
2018-05-29 16:38:10 -06:00
|
|
|
|
2018-01-05 17:32:57 -07:00
|
|
|
# TODO: Add alternatives to 'ip' in case of failure.
|
|
|
|
ip link set $__interface_reidentify__oldIdentifier name $__interface_reidentify__newIdentifier
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
2018-01-17 22:23:22 -07:00
|
|
|
# FLUXSCRIPT END
|