Differences in ARP scanning macOS / Raspbian / Python results - python

With the help of some tutorials I wrote a Python script that lists network details of the devices in the network.
I however only detect certain devices if I run the Python script. I already saw when searching that certain devices (e.g. iOS devices) don't respond (but are occasionally detected), but the strange thing is:
macOS (Catalina, 10.15.2) does find these devices with "arp -a"
Raspbian (10, buster) also finds a number of devices when doing "arp -a" but does not find the iOS devices
Python (3/2.7) does not find the Arduino devices which Raspbian and macOS do find, I run the Python script from the mac.
Running the Python script on the Pi does give me the Arduino devices as well.
I use Scapy for the ARP messages in Python (based on this tutorial: https://www.thepythoncode.com/article/building-network-scanner-using-scapy):
def find_devices(gateway):
_ip_items = gateway.split(".")
target_ip = "%s.%s.%s.%s" % (_ip_items[0], _ip_items[1], _ip_items[2], "0/24")
# IP Address for the destination
# create ARP packet
arp = ARP(pdst=target_ip)
# create the Ether broadcast packet
# ff:ff:ff:ff:ff:ff MAC address indicates broadcasting
ether = Ether(dst="ff:ff:ff:ff:ff:ff")
# stack them
packet = ether/arp
result = srp(packet, timeout=3, verbose=0)[0]
# a list of clients, we will fill this in the upcoming loop
clients = []
for sent, received in result:
# for each response, append ip and mac address to `clients` list
clients.append({'ip': received.psrc, 'mac': received.hwsrc})
print("{:16} {}".format(received.psrc, received.hwsrc))
Of course I could use ARP -A directly (e.g. execute that from Python and parse the results), but I am much more curious about the reasons that the Raspberry Pi seems to detect more devices than the Python code and macOS detects even more (are they cheating and using bonjour e.g.?) but how come both detect the Arduino's (Nano 33 IoT boards with MQTT pub/sub code on them) while the script on macOS does not detect those.

I actually found out that I can get even the iOS devices and Arduino devices when I chance the timeout to a much larger value (e.g. 15). It is still much more slow than macOS (arp -a gives an instant response) but they probably draw it from some cache or something(?). It does produce all devices that I see in arp -a also from the Python code even though it then becomes really slow.

Related

Python get MAC addresses of devices when not on wifi

I am working on a project that requires knowing the bluetooth or wifi MAC adresses of local IOS devices but whenever the IOS device is not in the settings screen for bluetooth, the device is not discoverable. This is a problem because the program needs to scan all local IOS devices no matter if the settings screen is open. I am considering switching to using wifi mac addresses if there is a way to extract them when not being connected to the same network or even on a network at all just in the same area. It wouldn't need to be long-range only find some sort of always-broadcasting unique identifier short distances (I know IP adresses can change so I avoided using them) that can be found without being connected to a certain wifi or being discoverable on bluetooth. Is there a solution that would work with python and IOS? Thanks for any responses in advance!

i am sending commands through serial port in python but they are sent multiple times instead of one

i am sending some commands having particular response serially using com port..the commands are kept in a file..i am reading each command through the file line by line and sending it serially over the com port..but when i am seeing it from the receiver end using Magic Terminal(Software)..i found that each command is going multiple times..which i am sending only one time..i have made a code in pycharm..and in the console i am seeing that command is going only once but from the uart receiving end the story is something else..i am stuck with this problem..i have maintain the same baudrate and everything but not able to diagnose the issue..
github link for the code is: https://github.com/AkshatPant06/Akshat-Pant/blob/master/cmd%20list
def recvResponse():
ser.write(serial.to_bytes(intCmd))
time.sleep(1)
data_recv=ser.read(2)
return data_recv
this i have used to receive the 2 byte response..
There seems to be nothing wrong with your code. At least to the extent I could reproduce, it only sends the command once (I tried your function after setting up my serial port in loopback).
I cannot say for sure but it might be that the terminal you're using has two windows, one for input and another one for output and somehow you're getting confused with what is in and out of your port.
One easy way to deal with this kind of issue is to use a sniffer on your port. You can do that combining com0com and Termite on Windows, as I recently explained here.
As you can see there is only one window on this terminal, and after setting up the forwarding you'll everything that comes in and out of your port. That should make it easier to see what your code is writing and reading.
To give you a conventional scenario to apply the sniffer trick you can refer to the following screenshot:
In this case, we have two real serial ports on a computer. On the first (COM9) we are running a Modbus server (you can imagine it as a bunch of memory addresses, each of one storing a 16-bit number). On COM10 we have a client that is sending queries asking for the contents of the first 10 addresses (called registers using the Modbus terminology). In a general use case, we have those ports linked with a cable, so we know (theoretically) that the client on COM10 is sending a data frame asking for those ten registers and the server on COM9 is answering with the numbers stored on those registers. But we are only able to see the contents on the server (left side of the picture) and what the client is receiving (right). What we don't see is what is traveling on the bus (yeah, we know what it is, but we don't know exactly how the Modbus protocol looks like on the inside).
If we want to tap on the bus to see what is being sent and received on each side we can create a couple of virtual ports with com0com and a port forwarding connection with Termite, something like the following screenshot:
Now we have moved our Modbus server to one of the virtual serial ports (COM4 in this case). After installing com0com we got (by default, but you can change names or add more port pairs, of course) a pair of forwarded ports (COM4<-->COM5). Now, if we want to see what is circulating through the ports we open Termite (bottom-right side of the picture) and set up another port forwarding scheme, in this case from virtual port COM5 to the real port COM9.
Finally (and exactly the same as before we were sniffing), we have COM9 connected together with COM10 with a cable. But now we are able to see all data going to and fro on the bus (all those HEX values you see on Termite displayed with the green/blue font).
As you can see, this will offer something similar to what you can do with more professional tools.

USB ports gets interchanged on RaspberryPi everytime i reboot my RaspberryPi

I am stuck at a very unusual problem. I am making a project in which i am using two USB ports of RaspberryPi for attaching a GPS module and a S2 Xbee. The code of python i am using is using both the GPS and the S2 Xbee. The biggest problem i am facing is whenever i switch off my raspberryPi and start it again, the ports gets interchanged. Basically, for GPS i am using USB0 and for Xbee i am using USB1, but they gets interchanged.
Please suggest me some solution. I searched a lot but could not find much.
Each USB device has a unique vendor id and product id combination. The command 'lsusb -v' or 'udevadm info --query=all -n /dev/bus/usb/001/00x' x = [1..4] on raspi, will identify the device name where they are mounted by the raspi.
Here is another way without starting linux commands from python:
Simple way to query connected USB devices info in Python?

Capturing CTS frame with Scapy and python

I have my WiFi card in monitoring mode and listening packages from Python with Scapy. I'm also sending RTS request and hoping to pickup CTS reply but it never happens. (I expect it to be type 1, subtype=12.)
Am I sending RTS package the right way? (MAC is just sample). Should I use rt1() instead of sendp() to get response and how? (I only find samples with IP and I have just a MAC)
Doto11 = Dot11(type=1, subtype=11, addr1="ee:11:ee:11:ee:12", ID=0x99)
pkt = RadioTap()/Doto11
sendp(pkt, iface=wlan1mon, realtime=True)

Is it possible to find out which USB port a MIDI device is connected to in portmidi / pyportmidi

I'm connecting a several identical USB-MIDI devices and talking to them using Python and pyportmidi. I have noticed that when I run my code on Linux, occasionally the MIDI ports of the devices are enumerated in a different order, so I send messages to the wrong devices. As the devices do not have unique identifiers, I am told that I should identify them by which USB port they are connected to.
Is there any way to retrieve this information? My app will run on Linux, but Mac OS support is useful for development.
It's annoying because they usually enumerate in a sensible order - the first device in the hub is the first device in portmidi, but sometimes they don't - usually the first 2 devices are switched. I have to physically move the devices without unplugging to fix them.
lsusb should do the trick. All devices and their respective hubs are listed there.

Categories