create_ap

create a nat-ed wifi ap
git clone git://git.2f30.org/create_ap
Log | Files | Refs | README | LICENSE

commit c04cce072c13484e956909f22ce9ea2c3d497878
parent 0f5a503c7961a2f165034c34fd6426ca8c291467
Author: oblique <psyberbits@gmail.com>
Date:   Tue,  9 Jun 2015 22:50:14 +0300

Merge pull request #82 from hashken/list_connected

Add switch to list connected clients
Diffstat:
Mbash_completion | 8++++++--
Mcreate_ap | 106++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 106 insertions(+), 8 deletions(-)

diff --git a/bash_completion b/bash_completion @@ -123,11 +123,15 @@ _create_ap() { ;; --stop) 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 ;; + --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 @@ -56,8 +56,11 @@ usage() { echo " --daemon Run create_ap in the background" 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 " get them with --list" - echo " --list Show the create_ap processes that are already running" + echo " get them with --list-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 " --config <conf_file> 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","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" @@ -987,6 +1065,16 @@ while :; do --list) shift 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) shift @@ -1027,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 @@ -1053,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 @@ -1271,8 +1365,8 @@ echo "Config dir: $CONFDIR" echo "PID: $$" echo $$ > $CONFDIR/pid -# to make --list work from any user, we must give read -# permitions to $CONFDIR and $CONFDIR/pid +# to make --list-running work from any user, we must give read +# permissions to $CONFDIR and $CONFDIR/pid chmod 755 $CONFDIR chmod 444 $CONFDIR/pid