Python Forum
Remote control a machine via OS module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remote control a machine via OS module
#1
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.
Reply
#2
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.
Reply
#3
(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?
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to gather information from remote host using ansible module in python amritjsr 0 2,005 Jul-20-2019, 01:17 AM
Last Post: amritjsr
  help need with running python script on remote windows machine onenessboy 2 11,015 Dec-14-2018, 03:02 PM
Last Post: onenessboy
  Remote control of the Visual Studio 2013 debug menu using Python MariusTo 0 2,449 Jan-17-2018, 04:58 PM
Last Post: MariusTo
  Possible to RDP onto a remote machine? jonesin1974 5 6,452 Dec-08-2017, 11:42 PM
Last Post: jonesin1974
  remote monitoring and control usb adapters Fran_3 0 2,249 Aug-24-2017, 07:55 PM
Last Post: Fran_3

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020