SSH and SCP from windows with Python 3 - python

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.

Related

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.

send SCPI command through python socket module

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.

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

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.

How to send file to serial port using kermit protocol in python

I have device connected through serial port to PC. Using c-kermit I can send commands to device and read output. I can also send files using kermit protocol.
In python we have pretty nice library - pySerial. I can use it to send/receive data from device. But is there some nice solution to send files using kermit protocol?
You should be able to do it via the subprocess module. The following assumes that you can send commands to your remote machine and parse out the results already. :-)
I don't have anything to test this on at the moment, so I'm going to be pretty general.
Roughly:
use pyserial to connect to the remote system through the serial port.
run the kermit client on the remote system using switches that will send the file or files you wish to transfer over the remote systems serial port (the serial line you are using.)
disconnect your pyserial instance
start your kermit client with subprocess and accept the files.
reconnect your pyserial instance and clean everything up.
I'm willing to bet this isn't much help, but when I actually did this a few years ago (using os.system, rather than subprocess on a hideous, hideous SuperDOS system) it took me a while to get my fat head around the fact that I had to start a kermit client remotely to send the file to my client!
If I have some time this week I'll break out one of my old geode boards and see if I can post some actual working code.

Categories