Merge pull request #82 from hashken/list_connected

Add switch to list connected clients
This commit is contained in:
oblique 2015-06-09 22:50:14 +03:00
commit c04cce072c
2 changed files with 106 additions and 8 deletions

View File

@ -123,11 +123,15 @@ _create_ap() {
;; ;;
--stop) --stop)
local stop_awk_cmd='$1 ~ /^[0-9]+$/' local stop_awk_cmd='$1 ~ /^[0-9]+$/'
opts=$("$1" --list | awk "$stop_awk_cmd") opts=$("$1" --list-running | awk "$stop_awk_cmd")
;; ;;
--list) --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
;; ;;

106
create_ap
View File

@ -56,8 +56,11 @@ usage() {
echo " --daemon Run create_ap in the background" echo " --daemon Run create_ap in the background"
echo " --stop <id> Send stop command to an already running create_ap. For an <id>" echo " --stop <id> Send stop command to an already running create_ap. For an <id>"
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" echo " get them with --list-running"
echo " --list 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","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"
@ -987,6 +1065,16 @@ while :; do
--list) --list)
shift shift
LIST_RUNNING=1 LIST_RUNNING=1
echo -e "WARN: --list is deprecated. Use --list-running instead.\n" 2>&1
;;
--list-running)
shift
LIST_RUNNING=1
;;
--list-clients)
shift
LIST_CLIENTS_ID="$1"
shift
;; ;;
--no-haveged) --no-haveged)
shift shift
@ -1027,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
@ -1053,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
@ -1271,8 +1365,8 @@ echo "Config dir: $CONFDIR"
echo "PID: $$" echo "PID: $$"
echo $$ > $CONFDIR/pid echo $$ > $CONFDIR/pid
# to make --list work from any user, we must give read # to make --list-running work from any user, we must give read
# permitions to $CONFDIR and $CONFDIR/pid # permissions to $CONFDIR and $CONFDIR/pid
chmod 755 $CONFDIR chmod 755 $CONFDIR
chmod 444 $CONFDIR/pid chmod 444 $CONFDIR/pid