Detect if connected to the internet using a tethered phone in Python - python

I was wondering if there is a way to detect, from a python script, if the computer is connected to the internet using a tethered cell phone?
I have a Python script which runs a bunch of network measurement tests. Ideally, depending on the type of network the client is using (ethernet, wifi, LTE, ...) the tests should change. The challenge is how to get this information from the python client, with out asking the user to provide it. Especially detect tethering.

Normally not - from the computer's prospective the tethered cell phone is simply just another wifi router/provider.
You might be able to detect some of the phone carrier networks from the traceroute info to some known servers (DNS names or even IP address ranges of network nodes - they don't change that often).
If you have control over the phone tethering you could also theoretically use the phone's wifi SSID (or even IP address ranges) to identify tethering via individual/specific phones (not 100% reliable either unless you know that you can't get those parameters from other sources).

In case anyone is interested, what I ended up doing was keeping the phone connected to my machine and used ADB to get this info. The command I run is:
adb shell dumpsys netstats
This is also a useful link.

Related

How to distinguish identical USB-to-serial converter?

I have 2 identical USB-to-serial adapter cables (pretty much like this one) to connect a desktop PC under Ubuntu and some RS232 devices.
I develop a python software to pilot these devices.
I need to find a way to identify which one of the 2 adapters I am connected to.
I know about python's serial.tools.list_ports.comports() function, but all the settings are the exact same for both adapters (see capture below). Except the device, but it may change depending on the plugging order.
How can I change some settings' field to make both adapter pythonically distinguishable? Is it possible to write my own serial_number for example?
To solve this problem there are some alternative paths to serial devices in Linux.
There's either
/dev/serial/by-id/ and /dev/serial/by-path
variant to access your devices.
If this is always for a specific serial device, the normal way to do this is to use the udev program to create symlinks for you in /dev. There's a lot of different options for how to do this, either based on what physical port it's plugged into or based off of attributes of the device(e.g. serial number).
FTDI based devices all have a serial number associated with them, but since yours is a Prolific they don't have a serial number, so this becomes a bit harder. However, since we can use udev to create a symlink based on where it is plugged in, something like the following should work(put this in a file seen by udev, e.g. /etc/udev/rules.d/80-my-converter.rules):
SUBSYSTEM=="usb", KERNELS=="2-1.8.3", SYMLINK+="device_1"
SUBSYSTEM=="usb", KERNELS=="2-1.8.1.3", SYMLINK+="device_2"
The KERNELS parameter will have to change depending on where exactly you plug the serial device into and will be specific to your system. You can get a list of the udev parameters on your device by running the following:
udevadm info -a -n /dev/ttyUSB2
This page has more information on writing udev rules.

Making monthly reporting program about Cisco product

I'm running a network infra consisted with Cisco product.
I'm trying to make a ordinary network monthly reporting program with Python.
I have never made any program before, So I think you guys have better way to do it.
Check my plan and teach me that is a good idea or not.
my plan as below.
Connect to Network Machine and collect log
telnet x.x.x.x
collect "show command"
save that log as text file
※ this action may run in telnet client program(like a SecureCRT)
open the text file and make a report
ex) combine "Show interface status" / "Show ip interface brief" / "Show ip arp" / "Show mac address-table" to make "Interface/IP/Mac/Port/Status" Report
This is my approximate plan.
Do you think it is efficient way? I need your teaching.
and I wonder how do you guys do similar job in datacenter.
We use Monitoring Tools for such tasks.
There are plenty of Open Source applications such as NeDi, Cacti and so on.
Why do you want to use "show" commands instead of using SNMP to get the device information?
Also I'd recommend to use SSH instead of telnet (for security reasons).
Cheers

Exchange internal ip blindly using python

I would like to share my internal IP between two devices on a network (using python 3).
Let's say I have my phone, and my computer. Both connected to the same network. I need to run a client and server script to connect the two but in order to do that, my phone (client) needs the ip of the computer (server).
The IP of the computer changes all the time (school wifi, nothing I can do about it) and even so I would like this to work instantly when connected to a new router without having to manually enter the IP.
One more thing, due to the huge amounts of devices on the network, mapping every device and finding the computer name to get the IP will take too long for its purpose.
Please use DNS for the purpose, or assign static addresses to your devices, and use the defined static addresses in your scripts.
In case anyone was wondering. I take this question as unsolvable, but, in order to solve my issue, I have set my computer to upload its internal IP to a server each time it connects to a new network. My phone then reads the server.

Selecting a Network Interface with Python

I want to connect to two different GoPro cameras using my computer's integrated network card and a USB drive wifi adapter (GoPros connect through Wifi). both GoPros receive commands through the same IP address, 10.5.5.9. So, if I get_ip_address on both available cameras, they return the same result - and as far as I can see, this invalidates using socket to select an interface, which seems to be the most common/popular response to question like this on the interwebs. What I usually see for a potential solution is
s1 = socket.socket()
s1.bind((get_ip_address('gopro1'), 0))
s1.connect(('url', 0))
...except that, as I said, the IP address is the same. So this doesn't select an interface at all; all it can do it open a connection from whatever the 'default' interface is. (Sending commands through this I think would be simple, since they're all sent as URLs, but if it only ever goes through one camera, than the point is defeated.)
I'm not sure where to go with this, or have many more ideas of how to phrase it differently for Google. How can I control traffic through two different interfaces with identical IP addresses?
Thanks!

Generating maximum wifi activity through 1 computer

I need to generate a very high level of wifi activity for a study to see if very close proximity to a transceiver can have a negative impact on development of bee colonies.
I have tried to write an application which spawns several web-socket server-client pairs to continuously transfer mid-sized files (this approach hit >100MB). However, we want to run this on a single computer connected to a wifi router, so the packets invariably end up getting routed via the loopback interface, not the WLAN.
Alternatively I have tried using a either simple ping floods and curling the router, but this is not producing nearly the maximum bandwidth the router is capable of.
Is there a quick fix on linux to force the traffic over the network? The computer we are using has both an ethernet and a wireless interface, and I found one thread online which suggested setting up iptables to force traffic between the two interfaces and avoid the loopback.
Simply sending packets as fast as possible to a random destination (that is not localhost) should work.
You'll need to use udp (otherwise you need a connection acknowledge before you can send data).
cat /dev/urandom | pv | nc -u 1.1.1.1 9123
pv is optional (but nice).
You can also use /dev/zero, but there may be a risk of link-level compression.
Of course, make sure the router is not actually connected to the internet (you don't want to flood a server somewhere!), and that your computer has the router as the default route.

Categories