how can I send raw packets over wifi without a network? - python

I am trying to send some data between 2 raspberry pi's over wifi without them being connected to a network.
I want it to work similar to the way beacons and probe requests work, where a broadcast can be sent out the wireless interface.
the data I want to send will be the hostname of the device and the time the packet was sent.
I've been trying to figure this out for the last few days but I cannot get anything to work without them being on the same network.
Is anybody able to point me in the right direction?
I'm not too fussed about what language I use. I have been trying python and C with little success.

Note: This is not really an answer, just some ideas to research, so feel free to edit or comment with corrections.
There are 6 modes a wifi adapter can operate in. From wikipedia:
Master (acting as an access point), Managed (client, also known as station), Ad hoc, Mesh, Repeater, and Monitor mode.
But I believe not all wifi adapters support all modes, so make sure you have suitable hardware.
I would suggest adhoc or mesh would be the most suitable to your purpose. With adhoc mode at least the devices would need to be configured beforehand to use the same SSID and channel. This may not apply to mesh mode.
A quick search yields a few links in regards to using Raspberry Pis in a mesh network:
http://hackaday.com/2012/11/14/mesh-networking-with-multiple-raspberry-pi-boards/
Wireless mesh networking on Raspberry Pi using batman-adv protocol
http://www.netlore.co.uk/airmesh/?page=about
I would also look at using the Dot11* family of packets in the excellent Python Scapy library (not to be confused with Scrapy). You should be able to craft custom packets and transmit them if the interface is in the right mode. Here's a project that creates fake access points that probably contains good info: https://github.com/rpp0/scapy-fakeap

Related

Sending data from PC to Raspberry Pi using Python

I am working on a project where I need to detect objects from IP camera on PC using TensorFlow and the data of the detected object to raspberry pi and control its GPIO pins accordingly.
I am using python on both ends.
I read somewhere to form a client-server network between the two.
I need some ideas on how to approach this problem. Thank you. I hope my question was clear.
Extra Information: I am currently using Raspberry pi on PC via ethernet cable. Also, both raspberry pi and my PC are connected to the same Wifi router. I hope this information helps.
You can use sockets to make the Raspberry Pi and your PC communicate. You can use socket.accept(conn, address) to accept a connection and conn.recv(buffer) to receive data.
I'm a big fan of using Redis for this type of thing. It has a small footprint, is simple to set up and is lightning fast. It is a "networked data structure server". "Networked" means that its clients can be on the same machine or anywhere on the network. The data structures it serves include:
strings
integers
atomic integers
lists
queues
sets
sorted sets
hashes
There are bindings for Python, C, C++, PHP, Ruby and rather brilliantly, the shell. That means you can inject values into queues, lists or sets directly from any Terminal on your network whilst your app is running, in order to test it or monitor it.
Example here.

Writing and routing over ethernet without IP

I have a project in which I need to setup a network that is essentially a bunch of Raspberry Pis connected through a router over ethernet, and have them talk to each other without using IP.
My challenge here is two folds, first, how can I write raw binary data to ethernet to pass my own custom payloads and have a custom parser on each end picking up and deserialising that data?
Second, and this is secondary for this post, if anyone has any ideas as to how I could use a router without using IP (aka setting up my own simple addressing protocol) this would be very welcomed. So far what I've sketched out is to procure myself a router than can be flashed, then have custom software on there running a custom protocol. However I'm not sure if this is even doable with off the shelf routers. Pointers are welcome.
Ideally I'd like to do all of this in python.
For your first question: asyncio comes as a standard library with Python. It can handle most of your communication needs, essentially acting as your communication stream for your binary data. Example implementation here.
For your second question: you can't go wrong with using IPv4. You could potentially implement something new but you'd probably go down a rabbit hole in doing so.

How to integrate different modbus devices on a web system?

I have two PLC modbus devices. These two are different companies.
The first one is that A will return the temperature and humidity. The second is that B will return five values. In both devices, the connection is modbus TCP.
One problem is that the same test software can be connected but the other cannot be connected. One device can be connected using pyModbus, but the other device is not working.
I also tested some software and python libraries. List is as follows
Device A
Software
modbus Poll (OK)
ModScan32 (NO)
Python Lib
pyModbus (OK)
EasyModbus (NO)
Device B
Software
modbus Poll (NO)
ModScan32 (OK)
Python Lib
pyModbus (NO)
EasyModbus (OK)
I don't know the difference between the two devices.
I want to integrate these two into the web system for monitoring, and new devices can be added in the future.
If there is a third device, I have to retest the connection to see which Python lib is available.
How can I implement this?
Learn more about the communication format of PLC devices?
Write a set of Python Libs for all devices?
Thank You.
Use one library for each, save the data somewhere centralized and view it with a different app. I don't see the problem. Also, have you tried re-trying the connection or reading after a few seconds? In my experience, modbus devices fail every so often to answer (or maybe is a library problem, I don't know) and a retry usually works.
For this, and it sounds like any good IOT framework would solve it, you should send the data from each device separately to one central point, like an IOT cloud solution, then you can build a webapp to view whatever data you have on the IOT framework, regardless of when or if it came in. Most frameworks will show you the old data as well if its not connected, so you can still display data, indicating it is stale or something to your user.

Reading Analog Data from sensor connected to USB (Python)

I may be confusing a few concepts here so any help is appreciated.
Q1: Is it possible to attach any sensor in the world to the USB on my computer as long as it gives me analog data, and read its output? (e.g. pH, temperature, oxygen sensor etc as long as it gives me analog data)
Q2: If so, then what is the simplest way in python for me read such data.
Comment: I am trying to bypass using PLC's, and trying to see if I can get the output from the sensor directly to the PC. (I do not have drivers for these sensors)
Actual Need: I have an oxygen sensor connected to my computer via a USB. The oxygen sensor is able to send out analog data. The obvious way is to go through a PLC. However, I would like a solution which by-passes PLC's so I can connect the sensor directly to my PC via USB.
The USB port on your computer cannot read analog data because USBs work with digital signals. You would need an analog-to-digital converter (ADC).
as #digitaLink answered, it is not possible directly via USB and yes, the obvious way is to use a PLC.
I would go the PLC way - in fact, I did it a few times in the past - and start with an Arduino and later develop a custom PCB, put it in a box and done.
Another possibility is to use a raspberry pi (or similar SBC), which has the GPIOs you can use for analog read.
Edit: there is another possibility.
The sensor you use now is _very_likely_ a PLC in itself, that is, the sensor is attached to a microcontroller that uses the USB port for serial communication. Now, the drivers you are missing do nothing else but decode the data coming through the serial port. Take a look inside your harware and try to find out what components there are in.
So what you could do is to try to find out how to communicate with the sensor via a serial terminal. It is probably possible to monitor serial communication (although I must admit, I don't know how to do that), reverse engineer the code and write your own driver in python. You could learn a lot from this, even if you don't succeed.

Stop packets at the network card

This is the problem I'm trying to solve,
I want to write an application that will read outbound http request packets on the same machine's network card. This would then be able to extract the GET url from it.On basis of this information, I want to be able to stop the packet, or redirect it , or let it pass.
However I want my application to be running in promiscuous mode (like wireshark does), and yet be able to eat up (stop) the outbound packet.
I have searched around a bit on this..
libpcap / pcap.h allows to me read packets at the network card, however I haven't yet been able to figure out a way to stop these packets or inject new ones into the network.
Certain stuff like twisted or scapy in python, allows me set up a server that is listening on some local port, I can then configure my browser to connect to it, using proxy configurations. This app can then do the stuff.. but my main purpose of being promiscuous is defeated here..
Any help on how I could achieve this would be greatly appreciated ..
I'd suggest that you approach this at the application layer and use a transparent proxy (e.g. squid) and iptables based interception of outbound port-80 traffic.
The reason I suggest this is that that it will avoid issues with the request being split between packets.
However, if you still want to go ahead with packet interception, you can do it in userspace using netfilters in netlink. I believe there are python wrappers for libnl around.
Essentially you create an iptables rule pointing to "QUEUE" for the traffic you want to intercept and write a program using a netlink library to process the queue, accepting, rejecting and/or modifying packets.
Using pcap you cannot stop the packets, if you are under windows you must go down to the driver level... but you can stop only packets that your machine send.
A solution is act as a pipe to the destination machine: You need two network interfaces (without address possibly), when you get a packet that you does not found interesting on the source network card you simply send it on the destination network card. If the packet is interesting you does not send it, so you act as a filter. I have done it for multimedia performance test (adding jitter, noise, etc.. to video streaming)
You are confusing several things here:
"Promiscuous" usually refers to a mode of a hardware ethernet network card where it delivers all packets in its collision domain up to the kernel network stack and have it sort out delivery (vs. just unicast to given MAC, subscribed multicast, and broadcast in normal operating mode of the card). This is on the receive path.
All the data outbound from your machine will go through (one of) the network cards on the machine, so "promiscuous" does not at all apply here.
You are working on filtering TCP-based protocol (HTTP), but talk in terms of packets. This is wrong. TCP connection is a stream that could be (as far as socket readers and writers are concerned) arbitrarily split into IP datagrams. That URL from HTTP request header could be split across multiple link-layer frames. You would have to stitch them back together and parse the stream anyway. Then you have no chance even at that if SSL is in use.
If you are interested in HTTP filtering then read HTTP RFCs, and read existing open-source code, e.g. squid, nginx, etc.
If you are digging through network stack for better understaning then read W. Richard Stevens books, look into existing code in open-source operating systems, check out BPF and netlink.
Hope this clears it a little.
I have implemented this module in Windows by using two separate NICs and using a socket/pipe(whatever you like) between them in this thread

Categories