1

So basically I have this remote computer with a bunch of files. I want to run unix commands (such as ls or cat) and receive them locally.

Currently I have connected via python's sockets (I know the IP address of remote computer). But doing:

data = None
message = "ls\n"
sock.send(message)
while not data:
    data = sock.recv(1024) <- stalls here forever
    ...

is not getting me anything.

5
  • Are you trying to create an SSH clone? Commented Apr 21, 2016 at 7:05
  • did you open some shell on the other end before sending the ls command? is someone listens on the other side? Commented Apr 21, 2016 at 7:06
  • @EhteshChoudhury Yeah basically. Srgrn: There is someone listening. I was able to log in before this block of code. Commented Apr 21, 2016 at 7:06
  • You can take a look at the pexpect lib : pexpect.readthedocs.org/en/stable/api/pxssh.html i already used it to interact with a remote shell and the pxssh class works fine. Commented Apr 21, 2016 at 7:08
  • I hope your remote host is not connected to the internet Commented Apr 21, 2016 at 7:17

3 Answers 3

1

There is an excellent Python library for this. It's called Paramiko: http://www.paramiko.org/

Paramiko is, among other things, an SSH client which lets you invoke programs on remote machines running sshd (which includes lots of standard servers).

Sign up to request clarification or add additional context in comments.

2 Comments

Is there nothing builtin that would accomplish this feat? I don't want to load in a library to just run ls and cat stuff (I dont need complicated stuff).
There is nothing built into Python for this. Paramiko is a widely used library; using it when it is purpose-built to accomplish the task at hand should not be controversial. If you still refuse to use it, you can try subprocess.check_call('ssh hostname ls') which is a worse solution overall but uses built-in stuff.
1

You can use Python's subprocess module to accomplish your task. It is a built-in module and does not have much dependencies.

For your problem, I would suggest the Popen method, which runs command on remote computer and returns the result to your machine.

out = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    t = out.stdout.read() + out.stderr.read()
    socket.send(t)

where cmd is your command which you want to execute.

This will return the result of the command to your screen. Hope that helps !!!

2 Comments

@dcheng what erorr are you getting?
I put in ls in cmd but that seems to try to ls my local computer's folder and then send the results of that through to the other side. (I get some really weird looking errors like File <localfile> not found).
0

This is what I did for your situation.

In terminal 1, I set up a remote shell over a socket using ncat, a nc variant:

$ ncat -l -v 50007 -e /bin/bash

In terminal 2, I connect to the socket with this Python code:

$ cat python-pass-unix-commands-socket.py

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('', 50007))

sock.send('ls\n')
data = sock.recv(1024)
print data
sock.close()

$ python pass-unix-commands-socket.py

This is the output I get in terminal 1 after running the command:

Ncat: Version 6.40 ( http://nmap.org/ncat )
Ncat: Listening on :::50007
Ncat: Listening on 0.0.0.0:50007
Ncat: Connection from 127.0.0.1.
Ncat: Connection from 127.0.0.1:39507.
$

And in terminal 2:

$ python pass-unix-commands-socket.py
alternating-characters.in
alternating-characters.rkt
angry-children.in
angry-children.rkt
angry-professor.in
angry-professor.rkt
$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.