Skip to content
Related Articles

Related Articles

How to Execute Shell Commands in a Remote Machine in Python?

View Discussion
Improve Article
Save Article
  • Last Updated : 04 Jul, 2022

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.


  • 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:


import socket
# Create socket with socket class.
master = socket.socket()
# Host is the IP address of master
# machine.
host = ""
# 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.
# 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
    # If the command is exit, close the connection
    if command == "exit":
    # 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)
# close method closes the socket connection between
# master and slave.


Slave machine script:


import socket
import subprocess
# Create socket with socket class.
master = socket.socket()
# Host is the IP address of master machine.
host = ""
# 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()
    # If the command is exit, close the connection.
    if command == "exit":
    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.
# close method closes the connection.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!