From 3fd94ce9c3e7629826427b26eb7b55e367fcf12f Mon Sep 17 00:00:00 2001 From: Karthik K Date: Sun, 24 May 2015 19:26:58 +0530 Subject: [PATCH] Add switch --list-clients --list-clients prints the list of connected clients associated with . can either be a wifi iface name or pid of a create_ap instance. --- bash_completion | 4 +++ create_ap | 93 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/bash_completion b/bash_completion index b1a80d8..fad57b1 100644 --- a/bash_completion +++ b/bash_completion @@ -128,6 +128,10 @@ _create_ap() { --list-running) # No Options ;; + --list-clients) + local clients_awk_cmd='$1 ~ /^[0-9]+$/' + opts=$("$1" --list-running | awk "$clients_awk_cmd") + ;; --mkconfig) _use_filedir && return 0 ;; diff --git a/create_ap b/create_ap index 159cb76..2ec62d9 100755 --- a/create_ap +++ b/create_ap @@ -58,6 +58,9 @@ usage() { echo " you can put the PID of create_ap or the WiFi interface. You can" echo " get them with --list-running" echo " --list-running Show the create_ap processes that are already running" + echo " --list-clients List the clients connected to create_ap instance associated with " + echo " For an you can put the PID of create_ap or the WiFi interface." + echo " You can get them with --list-running" echo " --mkconfig Store configs in conf_file" echo " --config Load configs from conf_file" echo @@ -593,6 +596,7 @@ CONFIG_OPTS=(CHANNEL GATEWAY WPA_VERSION ETC_HOSTS HIDDEN SHARE_METHOD FIX_UNMANAGED=0 LIST_RUNNING=0 STOP_ID= +LIST_CLIENTS_ID= STORE_CONFIG= LOAD_CONFIG= @@ -780,6 +784,80 @@ list_running() { 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() { local PID x @@ -891,7 +969,7 @@ for ((i=0; i<$#; i++)); do fi 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 eval set -- "$GETOPT_ARGS" @@ -993,6 +1071,11 @@ while :; do shift LIST_RUNNING=1 ;; + --list-clients) + shift + LIST_CLIENTS_ID="$1" + shift + ;; --no-haveged) shift NO_HAVEGED=1 @@ -1032,7 +1115,8 @@ if [[ -n "$LOAD_CONFIG" && $# -eq 0 ]]; then fi # 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 exit 1 fi @@ -1058,6 +1142,11 @@ if [[ $LIST_RUNNING -eq 1 ]]; then exit 0 fi +if [[ -n "$LIST_CLIENTS_ID" ]]; then + list_clients + exit 0 +fi + if [[ $(id -u) -ne 0 ]]; then echo "You must run it as root." >&2 exit 1