Python 在远程机器上执行 Shell 命令

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

入门

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

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

方法:

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

主机脚本:

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

输出:

从机脚本:

import socket
import subprocess

# Create socket with socket class.
slave = socket.socket()

# Host is the IP address of master machine.
host = "127.0.0.1"

# 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 macine.
    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()

输出:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python可以通过paramiko库实现远程执行shell脚本。具体步骤如下: 1. 安装paramiko库:使用pip install paramiko命令进行安装。 2. 导入paramiko库:在Python脚本中使用import paramiko语句导入paramiko库。 3. 创建SSHClient对象:使用paramiko库中的SSHClient类创建SSHClient对象。 4. 连接远程主机:使用SSHClient对象的connect方法连接远程主机。 5. 执行shell脚本:使用SSHClient对象的exec_command方法执行shell脚本。 6. 关闭连接:使用SSHClient对象的close方法关闭连接。 示例代码如下: ``` import paramiko # 创建SSHClient对象 ssh = paramiko.SSHClient() # 自动添加主机名和密钥到本地主机的HostKeys对象 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接远程主机 ssh.connect(hostname='remote_host', port=22, username='username', password='password') # 执行shell脚本 stdin, stdout, stderr = ssh.exec_command('sh /path/to/script.sh') # 输出执行结果 print(stdout.read().decode()) # 关闭连接 ssh.close() ``` ### 回答2: Python可以通过调用Paramiko库来实现远程执行shell脚本。 Paramiko是一个基于Python的SSH协议库,可以实现SSH客户端和SSH服务器。以下是一个示例代码,用于远程执行shell脚本: ```python import paramiko # 远程主机信息 hostname = 'localhost' username = 'root' password = 'password' # 建立SSH客户端连接 client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname, username=username, password=password) # 远程执行shell脚本 stdin, stdout, stderr = client.exec_command('sh /path/to/script.sh') # 输出执行结果 print(stdout.read()) # 断开SSH连接 client.close() ``` 在这个代码中,我们首先通过Paramiko库建立了一个SSH客户端连接。然后,使用exec_command()方法来运行远程主机上的shell脚本。最后,我们利用stdout.read()方法读取了执行结果并进行输出。最后,我们使用close()方法关闭了与远程主机的连接。 值得注意的是,虽然使用Paramiko库可以方便地实现远程执行shell脚本,但为了安全和授权等原因,远程主机的安全设置可能会要求进行额外的调整。此外,如果要使用SSH密钥进行身份验证,可能需要编写其他代码。 ### 回答3: Python 是一种强大的编程语言,可以轻松地执行操作系统上的命令和脚本。如果你需要在远程服务器上运行一些 shell 脚本,Python 也可以实现。在这里,我们将探讨如何在 Python远程执行 shell 脚本。 首先,我们需要使用 Python 的 SSH 库进行远程登录到服务器。Python 的 Paramiko 库提供了 SSH 客户端的实现,可以帮助我们连接到服务器上的 SSH 服务。我们可以使用 pip 或 conda 安装 Paramiko 库: ``` pip install paramiko ``` 或 ``` conda install paramiko ``` 在安装 Paramiko 库之后,我们需要导入库并使用 ssh 客户端连接到远程服务器。以下是连接到服务器的 Python 代码示例: ```python import paramiko # Create an SSH client client = paramiko.SSHClient() # Automatically add the server key to the client client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Connect to the remote server client.connect('your_server_ip', username='your_username', password='your_password') ``` 这将创建一个 SSH 客户端,并使用提供的用户名和密码连接远程服务器。 现在,我们可以使用 ssh 客户端执行 shell 命令和脚本。以下是使用 Python 远程执行 shell 脚本的代码示例: ```python import paramiko # Create an SSH client client = paramiko.SSHClient() # Automatically add the server key to the client client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Connect to the remote server client.connect('your_server_ip', username='your_username', password='your_password') # Execute a shell script on the remote server stdin, stdout, stderr = client.exec_command('bash /path/to/your/script.sh') # Print the output of the command print(stdout.read().decode('utf-8')) # Close the SSH connection client.close() ``` 在这个示例中,我们使用 exec_command() 方法执行 shell 脚本。该方法返回三个文件流,分别是标准输入、标准输出和标准错误输出。我们可以使用 stdout.read() 方法获取输出,并使用 decode() 方法将其转换为 UTF-8 字符串。 最后,我们需要关闭 SSH 连接,以便释放资源并避免连接泄漏。 总之,使用 Python 执行远程 shell 脚本十分简单,我们只需要使用 Paramiko 库连接到服务器并使用 exec_command() 方法远程执行脚本即可。这种方法可以在远程服务器上执行任何类型的 shell 脚本,从而可以实现更高效和自动化的部署。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值