How to Execute Shell Commands in a Remote Machine in Python? Last Updated : 28 Sep, 2022 Comments Improve Suggest changes Like Article Like Report 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: Comment More infoAdvertise with us Next Article How to Execute Shell Commands in a Remote Machine in Python? K kabilan Follow Improve Article Tags : Python python-utility Practice Tags : python Similar Reads How to Execute Shell Commands in a Remote Machine using Python - Paramiko Paramiko is a Python library that makes a connection with a remote device through SSh. Paramiko is using SSH2 as a replacement for SSL to make a secure connection between two devices. It also supports the SFTP client and server model. Authenticating SSH connection To authenticate an SSH connection, 4 min read How to execute shell command in Ruby? Ruby is also known for its simplicity and versatile nature, it provides various methods for executing shell commands within your scripts. Whether you need to interact with the underlying operating system or automate tasks, Ruby offers several approaches to seamlessly integrate shell commands into yo 3 min read Executing Shell Commands with Python This article starts with a basic introduction to Python shell commands and why one should use them. It also describes the three primary ways to run Python shell commands.os.system()subprocess.run()subprocess.Popen()Â What is a shell in the os?In programming, the shell is a software interface for acce 4 min read How to Resolve Python Command Not Found Error in Linux Getting the "Python command not found" error in Linux usually means Python is missing or not properly set up on your system. This common issue can be caused by missing installations, incorrect PATH settings, or version mismatches. In this guide, youâll learn the exact steps to fix this error, so you 4 min read How to Execute a Python Script from the Django Shell? The Django shell is an interactive development and scripting environment that aids us in working with our Django project while experiencing it as if it were deployed already. It is most commonly used when one has to debug, test, or carry out one-time operations that require interaction with the Djan 4 min read Python | Execute and parse Linux commands Prerequisite: Introduction to Linux Shell and Shell Scripting Linux is one of the most popular operating systems and is a common choice for developers. It is popular because it is open source, it's free and customizable, it is very robust and adaptable. An operating system mainly consists of two par 6 min read How to Run Remote Command Execution on Powershell? From the Command Line Interface on Windows, the Command Prompt Application first comes to your mind. However, another great Command Line Interface is Windows Terminal i.e. Windows Powershell which can be also useful. If you want to perform Remote Command Execution on some other Remote Computers, the 5 min read How to Run Shell Commands in Kubernetes Pods or Containers In Kubernetes, we create pods by adding an extra layer of information on containers. This Kubernetes in short is known as K8s, an open-source container orchestration tool developed by Google. It is used to orchestrate the containers for bringing Agility in software deployment through scaling, and ma 6 min read Run function from the command line In Python Python is a flexible programming language with a wide range of uses that one of Pythonâs most useful ones is its ability to execute functions from the command line. Especially this feature is very useful when it comes to automation and scripting etc. In this article, Iâll explain how to execute a Py 4 min read Running Custom Django manage.py Commands in Tests Django's manage.py commands are powerful tools for performing various administrative tasks. Sometimes, you might want to call these custom commands directly from your test suite to test their functionality or to set up the environment for your tests. In this article, we will walk through the process 2 min read Like