From 50a4ee2c6d1ee9db9e9265f638e6c2747e9db29e Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Mon, 13 Mar 2023 11:15:31 -0400 Subject: [PATCH] Have a quick sample script to get LLA of USB nic peer --- misc/getusbnicaddr | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 misc/getusbnicaddr diff --git a/misc/getusbnicaddr b/misc/getusbnicaddr new file mode 100644 index 00000000..dd5d77e4 --- /dev/null +++ b/misc/getusbnicaddr @@ -0,0 +1,31 @@ +#!/usr/bin/python3 +import glob +import os +import select +import socket + + +def scan_nic(nicidx): + srvs = {} + s6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) + s6.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1) + s6.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) + s6.bind(('::', 1900)) + msg = b'M-SEARCH * HTTP/1.1\r\nHOST: [ff02::c]:1900\r\nMAN: "ssdp:discover"\r\nST: urn:dmtf-org:service:redfish-rest:1\r\nMX: 3\r\n\r\n' + s6.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, nicidx) + s6.sendto(msg, ('ff02::c', 1900)) + (rsp, peer) = s6.recvfrom(9000) + print('{}%{}'.format(peer[0], nicidx)) + +if __name__ == '__main__': + for nic in glob.glob('/sys/class/net/*'): + nic = os.path.basename(nic) + try: + driver = os.readlink('/sys/class/net/{}/device/driver/module'.format(nic)) + except: + continue + if 'cdc_ether' not in driver: + continue + idx = int(open('/sys/class/net/{}/ifindex'.format(nic)).read()) + break + scan_nic(idx)