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
Related
I am using python's socket module to send a simple SCPI command to my Keysight device, but I get a blank response. Does anyone know what I am doing wrong?
Here is my simple script.
import socket
s = socket.socket()
print("connecting")
s.connect(("192.168.25.7",5024))
print("sending")
s.settimeout(10)
s.send("*IDN?\n")
print(s.recv(2048))
I expect this script to return
➜ ~ python hack_na.py
connecting
sending
Agilent Technologies,E5063A,MY54100104,A.03.00
➜ ~
But if I run the script, I get this output instead.
➜ ~ python hack_na.py
connecting
sending
➜ ~
I can confirm that I get the desigred output with telnet.
➜ ~ telnet 192.168.25.7 5024
Trying 192.168.25.7...
Connected to 192.168.25.7.
Escape character is '^]'.
SCPI> *IDN?
Agilent Technologies,E5063A,MY54100104,A.03.00
SCPI>
telnet> Connection closed.
Thank you all in advance.
These two versions are not the same.
You're not seeing any output in the first case because you're not actually sending a complete message from the socket to the server. The telnet protocol uses \r\n to end lines, not just \n. So you'd need to do s.send("*IDN?\r\n").
But really, you should use a better tool for the job. Python's socket module is just direct bindings to the BSD socket interface, usually used for building low-level networking applications. As such, you'll need to worry about annoying details like the line-endings yourself. A better alternative is to use a higher-level library, more tailored for your purpose. telnetlib is a builtin module for operating as a telnet client, or you could use a third-party library explicitly for SCPI.
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
I have a Solaris 10 system, with Python 2.6.4, and I have to retrieve the files via the SFTP protocol, from the server, which does not allow the SSH logging in, i.e. only SFTP with RSA key is allowed. Could anyone please tell me:
is this possible at all?
is this possible with the above version of Python, or I need to upgrade it to 2.7.* work with the latest version of Twisted?
I have found this treat with the relevant information: twisted conch filetransfer
And this one: Python Twisted: twisted conch filetransfer verifyHostKey
But it is said there that Twisted first creates the SSH channel, and then establishes SFTP on top of it (forgive me for my possible misunderstanding and/or illiteracy), from the Twisted documentation:
Conch also provides an endpoint that is initialized with an already established SSH connection. This endpoint just opens a new channel on the existing connection and launches a command in that.
Will the same approach work in case you can not logging in via SSH? I.e. might it be possible to create an SSH channel if terminal SSH logging in is forbidden?
Are there any other approaches except Paramico, any other libraries that can help me in case of "No" to the above questions?
I know nothing about "Twisted". But I believe that you just have a terminology problem.
which does not allow the SSH logging in, i.e. only SFTP with RSA key is allowed
The above is nonsense. You cannot allow SFTP, but disallow SSH, because as you have already found in Twisted documentation, SFTP runs on top of SSH (this is true in general, that's nothing Twisted-specific).
What your server most probably really "does not allow" is "shell" access. That's not the same as as SSH. So the server allows SSH, allows SFTP, but does not allow shell.
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.
I've Been stuck here for days. I want to copy a file from my windows to a remote linux server and run the script there. I've tool for ssh and scp. from which I can call the linux server through command line but when I call it through python it gets hanged.
pro=subprocess.Popen('ssh user#server')
pro.communicate()
there is a blank screen. whatever I type then after appear to my screen.
I was hoping there should be a password prompt but there isn't any. I thought of using library like paramiko, pexpect, pyssh but none of them are supported in Python 3
Any help is highly appreciated.
http://docs.fabfile.org/en/1.0.1/index.html
I'm not sure it can be converted by 2to3
but it's rather simple to use:
from fabric.api import run, env
from fabric.context_managers import hide
from fabric.colors import green
with hide('status', 'running', 'output'):
print('Apache ' + env.host + ': ' + green(run('wget -q -O /dev/null http://localhost/ && echo OK')))
env.host comes from command line, twisted couch is another alternative but it's not yet ported to py3k
There was another question like this. Use netcat. 'man nc'. Use os.system() in python to spawn it on both client side and server side.
From the netcat manual page:
DESCRIPTION
The nc (or netcat) utility is used for just about anything under the sun
involving TCP or UDP. It can open TCP connections, send UDP packets,
listen on arbitrary TCP and UDP ports, do port scanning, and deal with
both IPv4 and IPv6. Unlike telnet(1), nc scripts nicely, and separates
error messages onto standard error instead of sending them to standard
output, as telnet(1) does with some.
Common uses include:
simple TCP proxies
shell-script based HTTP clients and servers
network daemon testing
a SOCKS or HTTP ProxyCommand for ssh(1)
and much, much more
This works great for both local or remote machines on an intranet and also internet if aware of the related issues (original question did not specify the meaning of 'remote'). Some examples are:
http://www.sans.org/security-resources/sec560/netcat_cheat_sheet_v1.pdf
"Netcat - The TCP/IP Swiss Army Knife - SANS"
http://www.sans.org/reading_room/whitepapers/tools/netcat-tcp-ip-swiss-army-knife_952
Note that sans.org teaches both foundational comp sci tools and security.
http://www.g-loaded.eu/2006/11/06/netcat-a-couple-of-useful-examples/ ; Note this example includes complete automated backup via netcat: "Netcat is extremely useful for creating a partition image and sending it to a remote machine on-the-fly"
http://www.stearns.org/doc/nc-intro.v0.9.html ; An example for making dirt simple remote logging.
As for the comment "but that isn't python": Don't reinvent the wheel when there are very good foundational utilities which have been ported to all O/Ss and have no other dependencies other than the underlying base O/S.