Add switch --list-clients

--list-clients <id> prints the list of connected clients associated with
<id>. <id> can either be a wifi iface name or pid of a create_ap
instance.
This commit is contained in:
Karthik K 2015-05-24 19:26:58 +05:30
parent 38db906a24
commit 3fd94ce9c3
2 changed files with 95 additions and 2 deletions

View File

@ -128,6 +128,10 @@ _create_ap() {
--list-running) --list-running)
# No Options # No Options
;; ;;
--list-clients)
local clients_awk_cmd='$1 ~ /^[0-9]+$/'
opts=$("$1" --list-running | awk "$clients_awk_cmd")
;;
--mkconfig) --mkconfig)
_use_filedir && return 0 _use_filedir && return 0
;; ;;

View File

@ -58,6 +58,9 @@ usage() {
echo " you can put the PID of create_ap or the WiFi interface. You can" echo " you can put the PID of create_ap or the WiFi interface. You can"
echo " get them with --list-running" echo " get them with --list-running"
echo " --list-running Show the create_ap processes that are already running" echo " --list-running Show the create_ap processes that are already running"
echo " --list-clients <id> List the clients connected to create_ap instance associated with <id>"
echo " For an <id> you can put the PID of create_ap or the WiFi interface."
echo " You can get them with --list-running"
echo " --mkconfig <conf_file> Store configs in conf_file" echo " --mkconfig <conf_file> Store configs in conf_file"
echo " --config <conf_file> Load configs from conf_file" echo " --config <conf_file> Load configs from conf_file"
echo echo
@ -593,6 +596,7 @@ CONFIG_OPTS=(CHANNEL GATEWAY WPA_VERSION ETC_HOSTS HIDDEN SHARE_METHOD
FIX_UNMANAGED=0 FIX_UNMANAGED=0
LIST_RUNNING=0 LIST_RUNNING=0
STOP_ID= STOP_ID=
LIST_CLIENTS_ID=
STORE_CONFIG= STORE_CONFIG=
LOAD_CONFIG= LOAD_CONFIG=
@ -780,6 +784,80 @@ list_running() {
mutex_unlock mutex_unlock
} }
list_clients_iface() {
local IFACE pid x
mutex_lock
for x in /tmp/create_ap.*; do
if [[ -f $x/pid ]]; then
pid=$(cat $x/pid)
if [[ -d /proc/$pid && "$LIST_CLIENTS_ID" -eq $pid ]]; then
IFACE=${x#*.}
IFACE=${IFACE%%.*}
echo $IFACE
break
fi
fi
done
mutex_unlock
}
list_clients_leaseinfo() {
local line lease_flag=0 count="$1" mac="$2"
local awk_cmd='{printf "%-20s %-18s %s\n", $2, $3, $4}'
while read line; do
if [[ "$mac" == "$(echo $line | awk '{print $2}')" ]]; then
printf "%2s " "$count"
echo $line | awk "$awk_cmd"
lease_flag=1
break
fi
done < /var/lib/misc/dnsmasq.leases
if [[ $lease_flag -eq 0 ]]; then
printf "%-20s %-18s %s\n" "$x" "*" "*"
fi
}
list_clients() {
# If PID is given, get the associated wifi iface
if [[ "$LIST_CLIENTS_ID" =~ ^[1-9][0-9]*$ ]]; then
local iface=$(list_clients_iface)
if [[ -n "$iface" ]]; then
LIST_CLIENTS_ID="$iface"
else
echo "ERROR: '$LIST_CLIENTS_ID' is not the pid of a running $PROGNAME instance" >&2
exit 1
fi
fi
if ! is_wifi_interface "$LIST_CLIENTS_ID"; then
echo "ERROR: '$LIST_CLIENTS_ID' is not a WiFi interface." >&2
exit 1
fi
if [[ $USE_IWCONFIG -eq 0 ]]; then
local awk_cmd='($1 ~ /Station$/) {print $2}'
local client_list=$(iw dev "$LIST_CLIENTS_ID" station dump | awk "$awk_cmd")
if [[ -z "$client_list" ]]; then
echo "No clients connected"
else
printf "%3s %-20s %-18s %s\n" "" "MAC" "IP" "Hostname"
fi
local mac count=1
for mac in $client_list; do
list_clients_leaseinfo $count $mac
((count++))
done
else
echo "ERROR: This option is not supported for the current driver" >&2
exit 1
fi
}
has_running_instance() { has_running_instance() {
local PID x local PID x
@ -891,7 +969,7 @@ for ((i=0; i<$#; i++)); do
fi fi
done done
GETOPT_ARGS=$(getopt -o hc:w:g:dnm: -l "help","hidden","ieee80211n","ht_capab:","driver:","no-virt","fix-unmanaged","country:","freq-band:","mac:","daemon","stop:","list","list-running","version","psk","no-haveged","mkconfig:","config:" -n "$PROGNAME" -- "$@") GETOPT_ARGS=$(getopt -o hc:w:g:dnm: -l "help","hidden","ieee80211n","ht_capab:","driver:","no-virt","fix-unmanaged","country:","freq-band:","mac:","daemon","stop:","list","list-running","list-clients:","version","psk","no-haveged","mkconfig:","config:" -n "$PROGNAME" -- "$@")
[[ $? -ne 0 ]] && exit 1 [[ $? -ne 0 ]] && exit 1
eval set -- "$GETOPT_ARGS" eval set -- "$GETOPT_ARGS"
@ -993,6 +1071,11 @@ while :; do
shift shift
LIST_RUNNING=1 LIST_RUNNING=1
;; ;;
--list-clients)
shift
LIST_CLIENTS_ID="$1"
shift
;;
--no-haveged) --no-haveged)
shift shift
NO_HAVEGED=1 NO_HAVEGED=1
@ -1032,7 +1115,8 @@ if [[ -n "$LOAD_CONFIG" && $# -eq 0 ]]; then
fi fi
# Check if required number of positional args are present # Check if required number of positional args are present
if [[ $# -lt 1 && $FIX_UNMANAGED -eq 0 && -z "$STOP_ID" && $LIST_RUNNING -eq 0 ]]; then if [[ $# -lt 1 && $FIX_UNMANAGED -eq 0 && -z "$STOP_ID" &&
$LIST_RUNNING -eq 0 && -z "$LIST_CLIENTS_ID" ]]; then
usage >&2 usage >&2
exit 1 exit 1
fi fi
@ -1058,6 +1142,11 @@ if [[ $LIST_RUNNING -eq 1 ]]; then
exit 0 exit 0
fi fi
if [[ -n "$LIST_CLIENTS_ID" ]]; then
list_clients
exit 0
fi
if [[ $(id -u) -ne 0 ]]; then if [[ $(id -u) -ne 0 ]]; then
echo "You must run it as root." >&2 echo "You must run it as root." >&2
exit 1 exit 1