Python Forum
Remote control a machine via OS module - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Remote control a machine via OS module (/thread-14755.html)



Remote control a machine via OS module - dindibo4 - Dec-15-2018

Hey, I made two python programs, in two different computers, that uses sockets to communicate between the two computers.
Then, I wanted to execute a terminal command at the client program and the command it self will be sent from the remote controller program, then the client will send the result back to the second program.
This is what I've tried:

The controller side:

import socket

s = socket.socket()
s.connect(('kali', 6655))

getInput = lambda: raw_input("client> ")

cmd = getInput()

while cmd != "###STOP###":
  s.send(cmd)
  reply = s.recv(1024)
  print("Output:\n\n{0}".format(reply))
  cmd = getInput()
s.send("###STOP###")
The Controlled side:

import socket
import os

s = socket.socket()
s.bind(('', 6655))
s.listen(1)

c, addr = s.accept()
print 'connected to: {0}'.format(addr)

cmd = ""
result = ""

getInput = lambda: c.recv(1024)
execute = lambda cmd: os.popen(cmd).read()

cmd = getInput()

while cmd != '###STOP###':
Those programs kinda work, it only worked for a few simple commands, for example: ls, pwd, cat, shutdown now. But when I tried more complex commands like: cd, rmdir, firefox. The program at the controlling side stopped working and I could not insert any input to send it to the controlled machine, it first executed those commands but afterwards, it was paused.

Does somebody know a different method that allows me to execute those commands or at least what's causing this problem?
Thanks in advance.


RE: Remote control a machine via OS module - Gribouillis - Dec-15-2018

Do you really have to send the command and get the output through these sockets? Is there a reason why you can't install a ssh server on kali and communicate through ssh? If you can do this, it will be much easier, you could use a python module such as plumbum or rpyc.


RE: Remote control a machine via OS module - dindibo4 - Dec-15-2018

(Dec-15-2018, 05:17 PM)Gribouillis Wrote: Do you really have to send the command and get the output through these sockets? Is there a reason why you can't install a ssh server on kali and communicate through ssh? If you can do this, it will be much easier, you could use a python module such as plumbum or rpyc.

personally, I prefer working with sockets.
But if you know a better way of doing this using ssh and those modules that you've mentioned, could you guide me how to make such a program?
or at least reference me to tutorials about this?


RE: Remote control a machine via OS module - Gribouillis - Dec-15-2018

You first need to run a ssh server on the remote machine and make sure you can connect this server from the local machine. This is a standard administrative task for your OS and it should be very easy to find documentation for this.