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 Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Like