Running shell commands on a Remote machine is nothing but executing shell commands on another machine and as another user across a computer network. There will be a master machine from which command can be sent and one or more slave machines that execute the received commands.
Getting Started
We will be using Websocket protocol to send shell commands to the slave machine and receive the output of commands. Websocket offers full-duplex communication over a single TCP connection in real-time. The Python provides a subprocess library an in-built library, that allows a new process to start and connect to their input, output, and error pipes. getoutput method in the subprocess library executes the command and returns the output, if an error occurs it also returns the errors.
You will easily understand its working with approach and implementation. So let’s begin.
Approach:
- Create Master machine script.
- Create a socket connection and listen for the slave machine socket.
- Accept the connection once the connection request is made.
- Use the input method to get a command from the user and encode it.
- Then use the socket connection to send the shell command.
- Then receive the output of the command.
- Create a Slave machine script.
- Create a Socket and connect it to the Master machine socket.
- Receive the command from the master.
- Execute the command with the get output method from the subprocess module.
- getoutput method returns the output of the executed command.
- Encode the output and send it to the master machine.
Master machine script:
Python3
import socket # Create socket with socket class. master = socket.socket() # Host is the IP address of master # machine. host = "0.0.0.0" # This will be the port that the # socket is bind. port = 8080 # binding the host and port to the # socket we created. master.bind((host, port)) # listen method listens on the socket # to accept socket connection. master.listen( 1 ) # This method accept socket connection # from the slave machine slave, address = master.accept() # When the slave is accepted, we can send # and receive data in real time while True : # input the command from the user print ( ">" , end = " " ) command = input () # encode the command and send it to the # slave machine then slave machine can # executes the command slave.send(command.encode()) # If the command is exit, close the connection if command = = "exit" : break # Receive the output of command, sent by the # slave machine.recv method accepts integer as # argument and it denotes no.of bytes to be # received from the sender. output = slave.recv( 5000 ) print (output.decode()) # close method closes the socket connection between # master and slave. master.close() |
Output:
Slave machine script:
Python3
import socket import subprocess # Create socket with socket class. slave = socket.socket() # Host is the IP address of master machine. host = "192.168.43.160" # This will be the port that master # machine listens. port = 8080 # connect to the master machine with connect # command. slave.connect((host, port)) while True : # receive the command from the master machine. # recv 1024 bytes from the master machine. command = slave.recv( 1024 ).decode() print (command) # If the command is exit, close the connection. if command = = "exit" : break output = "output:\n" # getoutput method executes the command and # returns the output. output + = subprocess.getoutput(command) # Encode and send the output of the command to # the master machine. slave.send(output.encode()) # close method closes the connection. slave.close() |
Output: