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.
Related
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.
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
Quick question: need pure python script of simple http client without using libs (only socket library possible).
Main task of this client is connect to server, receive greetings, sends get requests and read responses. Also it's good if this code will be compatible with Cython compiler.
I would recommend you using requests https://github.com/kennethreitz/requests package.
But your question looks like an assignment which shall teach you how is http working on TCP communication level. In such case I would recommend you
learn using http protocol over telnet or netcat
then learn TCP communication by Python and repeat, what you already know by telnet
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.
I'd like to search for a given MAC address on my network, all from within a Python script. I already have a map of all the active IP addresses in the network but I cannot figure out how to glean the MAC address. Any ideas?
You need ARP. Python's standard library doesn't include any code for that, so you either need to call an external program (your OS may have an 'arp' utility) or you need to build the packets yourself (possibly with a tool like Scapy.
I don't think there is a built in way to get it from Python itself.
My question is, how are you getting the IP information from your network?
To get it from your local machine you could parse ifconfig (unix) or ipconfig (windows) with little difficulty.
If you want a pure Python solution, you can take a look at Scapy to craft packets (you need to send ARP request, and inspect replies). Or if you don't mind invoking external program, you can use arping (on Un*x systems, I don't know of a Windows equivalent).
It seems that there is not a native way of doing this with Python. Your best bet would be to parse the output of "ipconfig /all" on Windows, or "ifconfig" on Linux. Consider using os.popen() with some regexps.
Depends on your platform. If you're using *nix, you can use the 'arp' command to look up the mac address for a given IP (assuming IPv4) address. If that doesn't work, you could ping the address and then look, or if you have access to the raw network (using BPF or some other mechanism), you could send your own ARP packets (but that is probably overkill).
You would want to parse the output of 'arp', but the kernel ARP cache will only contain those IP address(es) if those hosts have communicated with the host where the Python script is running.
ifconfig can be used to display the MAC addresses of local interfaces, but not those on the LAN.
Mark Pilgrim describes how to do this on Windows for the current machine with the Netbios module here. You can get the Netbios module as part of the Win32 package available at python.org. Unfortunately at the moment I cannot find the docs on the module.
as python was not meant to deal with OS-specific issues (it's supposed to be interpreted and cross platform), i would execute an external command to do so:
in unix the command is ifconfig
if you execute it as a pipe you get the desired result:
import os
myPipe = os.popen2("/sbin/ifconfig","a")
print(myPipe[1].read())