列出通过终端在热点连接的设备


16

我通过ap-hotspot连接我的热点,并且可以看到弹出的通知,显示新设备已连接设备已断开。(因为我想学习访问热点或不使用热点的特权。)

如何列出通过终端连接的设备?

Answers:


28

arp -a 应该会向您返回所有已连接设备的列表。


4
这也是arp -an有用的---避免尝试解析IP地址的长时间延迟。
Rmano 2014年

arp不会实时更新
路易斯

10

如果您需要更详细的列表,请将该脚本改编为来自webupd8ap-hotspot脚本:

#!/bin/bash

# show_wifi_clients.sh
# Shows MAC, IP address and any hostname info for all connected wifi devices
# written for openwrt 12.09 Attitude Adjustment
# modified by romano@rgtti.com from http://wiki.openwrt.org/doc/faq/faq.wireless#how.to.get.a.list.of.connected.clients

echo    "# All connected wifi devices, with IP address,"
echo    "# hostname (if available), and MAC address."
printf  "# %-20s %-30s %-20s\n" "IP address" "lease name" "MAC address"
leasefile=/var/lib/misc/dnsmasq.leases
# list all wireless network interfaces 
# (for MAC80211 driver; see wiki article for alternative commands)
for interface in `iw dev | grep Interface | cut -f 2 -s -d" "`
do
  # for each interface, get mac addresses of connected stations/clients
  maclist=`iw dev $interface station dump | grep Station | cut -f 2 -s -d" "`
  # for each mac address in that list...
  for mac in $maclist
  do
    # If a DHCP lease has been given out by dnsmasq,
    # save it.
    ip="UNKN"
    host=""
    ip=`cat $leasefile | cut -f 2,3,4 -s -d" " | grep $mac | cut -f 2 -s -d" "`
    host=`cat $leasefile | cut -f 2,3,4 -s -d" " | grep $mac | cut -f 3 -s -d" "`
    # ... show the mac address:
    printf "  %-20s %-30s %-20s\n" "$ip" "$host" "$mac"
  done
done

将其复制到PATH中的文件中-例如~/bin/show_wifi_clients,使用使其可执行chmod +x,然后使用。


一个不错的疯狂脚本,感谢分享:)...
George Udosen

1
中的变量printf " %-20s %-30s %-20s\n" $ip $host $mac"必须用双引号引起来才能正确打印。同样编辑了答案...
Magguu

@Magguu,您是对的,接受编辑。
Rmano

8

显示设备列表:(替换<interface>为wifi接口的接口名称)

iw dev <interface> station dump

如果您不知道wifi接口的名称,请使用以下命令找出接口名称:

iw dev

尽管此答案在当前状态下是好的,但仍可以改进。也许您可以添加一些示例输出,或者以其他方式解释此命令的作用?
卡兹·沃尔夫


0

这也可以获取设备的Mac供应商,还可以标记设备的Mac

需要python3.6

#!/usr/bin/python3.6   
import subprocess
import re
import requests

# Store Mac address of all nodes here
saved = {
    'xx:xx:xx:xx:xx:xx': 'My laptop',
}

# Set wireless interface using ifconfig
interface = "wlp4s0"

mac_regex = re.compile(r'([a-zA-Z0-9]{2}:){5}[a-zA-Z0-9]{2}')


def parse_arp():
    arp_out = subprocess.check_output(f'arp -e -i {interface}', shell=True).decode('utf-8')
    if 'no match found' in arp_out:
        return None

    result = []
    for lines in arp_out.strip().split('\n'):
        line = lines.split()
        if interface in line and '(incomplete)' not in line:
            for element in line:
                # If its a mac addr
                if mac_regex.match(element):
                    result.append((line[0], element))
    return result


def get_mac_vendor(devices):
    num = 0
    for device in devices:
        try:
            url = f"http://api.macvendors.com/{device[1]}"
            try:
                vendor = requests.get(url).text
            except Exception as e:
                print(e)
                vendor = None

        except Exception as e:
            print("Error occured while getting mac vendor", e)

        num += 1
        print_device(device, num, vendor)

def print_device(device, num=0, vendor=None):
    device_name = saved[device[1]] if device[1] in saved else 'unrecognised !!'

    print(f'\n{num})', device_name,  '\nVendor:', vendor, '\nMac:', device[1], '\nIP: ',device[0])

if __name__ == '__main__':
    print('Retrieving connected devices ..')

    devices = parse_arp()

    if not devices:
        print('No devices found!')

    else:
        print('Retrieving mac vendors ..')
        try:
            get_mac_vendor(devices)

        except KeyboardInterrupt as e:
            num = 0
            for device in devices:
                num += 1
                print_device(device, num)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.