📌  相关文章
📜  如何用Python在远程机器上执行 Shell 命令?

📅  最后修改于: 2022-05-13 01:55:11.089000             🧑  作者: Mango

如何用Python在远程机器上执行 Shell 命令?

在远程机器上运行 shell 命令只不过是在另一台机器上和作为计算机网络上的另一个用户执行 shell 命令。将有一台可以发送命令的主机和一台或多台执行接收到的命令的从机。

入门

我们将使用 Websocket 协议向从机发送 shell 命令并接收命令的输出。 Websocket 通过单个 TCP 连接实时提供全双工通信。 Python提供了一个子进程库和一个内置库,它允许新进程启动并连接到它们的输入、输出和错误管道。子进程库中的getoutput方法执行命令并返回输出,如果发生错误也返回错误。

您将很容易理解它的工作方式和实施方式。那么让我们开始吧。

方法:

  • 创建 Mater 机器脚本。
  • 创建套接字连接并侦听从机套接字。
  • 发出连接请求后接受连接。
  • 使用输入法从用户那里获取命令并对其进行编码。
  • 然后使用socket连接发送shell命令。
  • 然后接收命令的输出。
  • 创建一个从机脚本。
  • 创建一个 Socket 并将其连接到 Master 机器的套接字。
  • 接收来自主人的命令。
  • 使用来自子流程模块的 get 输出方法执行命令。
  • getoutput 方法返回执行命令的输出。
  • 对输出进行编码并将其发送到主机。

主机脚本:



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()


Python3
import socket
import subprocess
 
# Create socket with socket class.
master = 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()


输出:

从机脚本:

蟒蛇3

import socket
import subprocess
 
# Create socket with socket class.
master = 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()

输出: