How to implement the non-root and non-subprocess ping in Python? - python

I need to implement the ping in my Python application to get an RTT to certain hosts. There are several approaches to do that:
To use one of the PyPI packages (for example, pyping, ping, multiping, scapy and etc). But all of these packages create a raw socket to work with ICMP packets. It is not acceptable for me.
To create the raw socket myself. It is not acceptable because the reason above. I also tried to create the ICMP socket like this:
socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_ICMP)
It doesn't work at my machine.
To launch the ping utility via subprocess. But it works relatively slow and seems ugly.
TCP Ping. It is not acceptable in my project.
Can you suggest me a different way how to implement ping without root privileges and using ping utility in different processes? Maybe exists some different way how to get an RTT to certain host?
Python2.7, Ubuntu16

Related

analogs of socket in python

Can you advise me on the analogs of the socket library on Python? The task is this, I need to write a very simple script with which I could execute remote commands in cmd windows. I know how this can be implemented using the socket library, but I would like to know if there are any other libraries for such a case.
Sockets is a low level mechanism by which two systems can communicate each other. Your OS provides this mechanism, there's no analogs.
Next examples come from the application layer and they work with sockets in their lower communication layers: a socket open by your http server, usually 80 or 443 or a websocket open by your browser to communicate with your server. Or the DNS query that your browser executes when tries to resolve a domain name, also works with sockets between your PC and the DNS server.

Monitor network traffic while running a python script

I am trying to monitor network traffic coming in and out of my VM**. My VM is connected to a socket via TCP (IP, PORT) with the python socket library. Once connected, I am sending a stream of bytes to the socket and then close the connection. The VM runs Ubuntu 18.04 LTS. The connection is made in a VPN tunnel.
How do I capture the traffic for the source and destination while my python script runs? I have tried to work with scapy and Wireshark/pyshark, but the documentation I found did not help me a lot.
Does anyone have an idea how I could do this? I am using python 3
I use wireshark to capture packets, you can filter out the destination and source (as flags to), here's the docs. Is the VPN using some kind of security to encrypt information (such as TLS)?
In the filter insert:
ip.src==your.local.ip.addr && ip.dst==your.VM.ip.addr && ip.proto=="TCP"
What could happen is that the VM tries to get updates and wireshark can pickup a lot of packets, it can mess up your search for the information in the sockets (byte stream).
You also can try stop some Ubuntu services to prevent the internet use, but I cant tell you how to disable all.
Do you want to pickup that byte stream with a sniffer and convert into a person's eye?
If that's the case, it is advanced stuff I can't explain.
Hope I could help.

Pinging without calling system commands

I need a way to ping remote machines without calling system commands. And if possible, without admin or root access. Something that could work on any platform.
I had a quick look into python3 -m pip install pyping but importing it returns me the following :
ModuleNotFoundError: No module named 'core'.
Also it require root/admin access which I would like to avoid.
How should I proceed ?
An ICMP Ping is a "special" kind of networking that uses a raw socket. I don't super understand it myself, but TLDR is that it seems difficult to do without privilege escalation.
If you know for a fact a given TCP port on the target machine(s) are going to be open, you can just try to establish a TCP connection to that port. For example, if you can SSH into these machines, the standard SSH port (22) is usually open. You don't need to actually communicate - just establish a TCP connection to that port then drop it. See TcpCommunication
this is because the module was introduced for python 2,however if you want to use it with python3 you can manipulate it or use 2to3 module,this question was asked in unable to import pyping for python3

Python: send file to a server without third-party libraries?

I have a Python client behind a NAT and a python server with a public IP address. My job is to send a pcap file (the size of a few MB) from the client to a server, as well as a dictionary with some data.
Is there any easy way of doing this without resorting to third-party libraries (e.g. twisted, tornado)?
If not, what's the easiest alternative?
I thought I could send the pcap file through http so that it would be easier to read it on the server side, and perhaps I could do the same with the dictionary by first pickling it. Would it be a good solution?
(I have complete control on the server, where I can install whatever)
Is FTP a usable solution for you?
https://docs.python.org/2/library/ftplib.html
http://effbot.org/librarybook/ftplib.htm
If you can install software on the server, and the server allows HTTP connections, you can write your own simple HTTP server (Python has libraries for doing that). If not, the answer would depend on what services are available on the server.
you can use just the classic sockets with TCP!
You just need to send the file via TCP sockets and receive it in a TCP socket server.
Read it as a file, send it and receive it.
This might give you some tip about sockets: https://docs.python.org/2/library/socket.html
Later I'll post a little script in case you didn't solve your doubt.

Is there an easy way for a python script to bind to all ports on an IP address?

I'm writing a Python script which connects to remote hosts over a (super complicated) SOCKS/SSL tunnel. I am able to establish connections to IPs in a remote intranet on any port.
What I'm hoping to do is set up this python script to use IP addresses in the local loopback range (127.0.x.x) to become (maybe with the help of the hosts file) a 'replica' of the remote systems, and hence enable me to use applications which don't support proxies. The problem is that I don't always know what ports they're trying to connect to. It seems the only way to work this out is to bind sockets to all 65536 ports, which seems a little crazy. So two questions:
Is it crazy? Can I just set up a python list of sockets from 1-65536?
Or is there a better way I should be doing this? Can I monitor connections to an IP somehow and bind the ports just before they're needed?
I want to avoid using too much platform-dependent or non-python code if possible.
EDIT: To clarify, I'm only writing the client here - I have no control over the server. Believe me, if I had control over the server side of it I would not be doing it with SOCKS/SSL/CRAM :)
What about going lower level and interfacing a library designed for network analyzers like pycap?
This way you could detect all connection attempts and find the ports that you need to expose or may be you can just route the packets directly assuming the library in addition to packet detection can also do packet injection (pypcap page says this feature is experimental).
This would IMO make sense in python only for slow applications however...
Pycap seems to be developed for linux, but the core capturing is done by libpcap and for windows there is a similar library winpcap.
Matt,
If using windows your best shot is something like OpenVPN over the tunnel. OpenVPN requires only one TCP port/stream and gives you a pair of virtual interfaces with full connectivity.
[updated]
It may be possible using a TUN/TAP driver on the client side. See this unix version for ideas.

Categories