python37远程执行linux命令
-
要在Python 3.7中远程执行Linux命令,你可以使用paramiko库。Paramiko是一个Python的SSH库,用于在远程服务器上执行命令。
首先,你需要安装paramiko库。可以使用以下命令在Python中安装paramiko:
“`
pip install paramiko
“`安装完成后,在Python脚本中导入paramiko库:
“`python
import paramiko
“`接下来,你需要建立SSH连接到远程服务器。你需要提供远程服务器的IP地址、用户名和密码。在代码中,使用`SSHClient`类来建立SSH连接:
“`python
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(‘remote_server_ip’, username=’username’, password=’password’)
“`替换`remote_server_ip`为远程服务器的IP地址,`username`和`password`为你的登录凭据。
现在你已经建立了SSH连接,可以使用`exec_command`方法来远程执行Linux命令。以下是一个示例:
“`python
stdin, stdout, stderr = ssh.exec_command(‘ls’)
“`这个例子中,我们执行了`ls`命令。`exec_command`方法返回一个包含标准输入、标准输出和标准错误的元组。你可以使用`stdout.read()`方法获取标准输出的结果,使用`stderr.read()`方法获取标准错误的结果。
以下是一个完整的示例代码,展示如何在Python中远程执行Linux命令:
“`python
import paramikossh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(‘remote_server_ip’, username=’username’, password=’password’)stdin, stdout, stderr = ssh.exec_command(‘ls’)
output = stdout.read().decode(‘utf-8’)
error = stderr.read().decode(‘utf-8’)print(output)
print(error)ssh.close()
“`记得替换`remote_server_ip`、`username`和`password`为你远程服务器的实际凭据。
这样,你就可以使用Python 3.7中的paramiko库远程执行Linux命令了。
2年前 -
Python 3.7 提供了多种方法来远程执行 Linux 命令。下面是一些常用的方式:
1. paramiko 模块:paramiko 是一个基于 SSHv2 协议实现的 Python 模块,它可以实现远程执行命令、上传下载文件等功能。以下是一个使用 paramiko 远程执行 Linux 命令的示例代码:
“`python
import paramiko# 创建 SSH 客户端对象
client = paramiko.SSHClient()# 自动添加远程主机的 SSH 密钥
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 连接远程主机
client.connect(hostname=’hostname’, username=’username’, password=’password’)# 执行命令
stdin, stdout, stderr = client.exec_command(‘your_command’)# 获取命令输出
output = stdout.read().decode(‘utf-8’)# 关闭连接
client.close()
“`2. fabric 模块:fabric 是一个基于 paramiko 的高级部署工具,它提供了更简洁的接口来远程执行命令。以下是一个使用 fabric 远程执行 Linux 命令的示例代码:
“`python
from fabric import Connection# 创建 SSH 连接对象
conn = Connection(‘hostname’, user=’username’, connect_kwargs={‘password’: ‘password’})# 执行命令
result = conn.run(‘your_command’, hide=True)# 获取命令输出
output = result.stdout# 关闭连接
conn.close()
“`3. subprocess 模块:Python 的 subprocess 模块提供了一种以子进程的方式创建新进程并与其交互的方法。以下是一个使用 subprocess 远程执行 Linux 命令的示例代码:
“`python
import subprocess# 执行命令
cmd = [‘ssh’, ‘username@hostname’, ‘your_command’]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)# 获取命令输出
output, error = p.communicate()
output = output.decode(‘utf-8’)# 关闭子进程
p.wait()
“`4. pxssh 模块:pxssh 是 paramiko 库的一个封装,它提供了一个更简单的方式来远程执行命令和传输文件。以下是一个使用 pxssh 远程执行 Linux 命令的示例代码:
“`python
from pexpect import pxssh# 创建 SSH 对象
ssh = pxssh.pxssh()# 登录远程主机
ssh.login(‘hostname’, ‘username’, ‘password’)# 执行命令
ssh.sendline(‘your_command’)# 等待命令执行完成
ssh.prompt()# 获取命令输出
output = ssh.before.decode(‘utf-8’)# 关闭连接
ssh.logout()
“`5. fabric2 模块:fabric2 是 fabric 模块的后续版本,它提供了更多的功能和改进。以下是一个使用 fabric2 远程执行 Linux 命令的示例代码:
“`python
from fabric2 import Connection# 创建 SSH 连接对象
conn = Connection(‘hostname’, user=’username’, connect_kwargs={‘password’: ‘password’})# 执行命令
result = conn.run(‘your_command’, hide=True)# 获取命令输出
output = result.stdout# 关闭连接
conn.close()
“`以上是使用 Python 3.7 远程执行 Linux 命令的几种常用方式,根据实际需求选择适合自己的方法。
2年前 -
Python37 可以通过 SSH 远程连接 Linux 服务器,并执行命令。在 Python 中,可以使用 paramiko 模块来实现 SSH 连接和命令执行操作。
下面是使用 Python37 远程执行 Linux 命令的详细步骤:
**步骤1:安装 paramiko 模块**
首先,需要安装 paramiko 模块。在终端中运行以下命令来安装 paramiko:
“`
pip install paramiko
“`**步骤2:导入 paramiko 模块**
在 Python 中,首先需要导入 paramiko 模块来使用它提供的功能:
“`python
import paramiko
“`**步骤3:创建 SSHClient 对象**
接下来,需要创建一个 SSHClient 对象,该对象将用于建立 SSH 连接和执行命令。在创建 SSHClient 对象之前,我们需要设置一些连接参数,如远程主机的 IP 地址、端口号、用户名和密码等。
“`python
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(‘remote_host_ip’, username=’remote_username’, password=’remote_password’)
“`在上面的代码中,将 ‘remote_host_ip’ 替换为远程主机的 IP 地址, ‘remote_username’ 和 ‘remote_password’ 替换为远程主机的登录用户名和密码。
**步骤4:执行命令**
完成 SSH 连接之后,就可以执行远程 Linux 命令了。使用 SSHClient 对象的 exec_command() 方法来执行命令:
“`python
stdin, stdout, stderr = ssh.exec_command(‘command_to_execute’)
“`将 ‘command_to_execute’ 替换为要执行的 Linux 命令。exec_command() 方法返回三个文件对象:stdin、stdout 和 stderr。其中,stdout 是命令的标准输出,stderr 是命令的标准错误输出。
**步骤5:处理命令的输出**
使用 stdout 和 stderr 文件对象可以获取命令的输出结果。例如,可以使用 readlines() 方法读取命令的输出,并逐行打印:
“`python
output = stdout.readlines()
for line in output:
print(line.strip())
“`**步骤6:关闭 SSH 连接**
在命令执行完成之后,需要关闭 SSH 连接。
“`python
ssh.close()
“`执行完上述步骤之后,Python37 将会通过 SSH 连接远程主机并执行相应的 Linux 命令。你可以根据自己的需求,编写 Python 脚本来实现特定的操作。
这就是使用 Python37 远程执行 Linux 命令的基本方法和操作流程。希望对你有帮助!
2年前