python执行linux命令返回结果

不及物动词 其他 401

回复

共3条回复 我来回复
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    在Python中执行Linux命令并获取结果,可以使用subprocess模块。下面是一个示例代码:

    “`
    import subprocess

    def run_command(command):
    # 执行命令并返回结果
    try:
    result = subprocess.run(command, shell=True, capture_output=True, text=True)
    output = result.stdout
    return output.strip()
    except Exception as e:
    return str(e)

    # 示例:执行ls命令并打印结果
    output = run_command(“ls”)
    print(output)

    # 示例:执行pwd命令并打印结果
    output = run_command(“pwd”)
    print(output)
    “`

    在上面的示例代码中,我们定义了一个`run_command`函数,接受一个命令作为参数。函数内部使用`subprocess.run`调用Linux命令,并将输出结果赋值给`result`变量。然后,我们从`result.stdout`中获取命令执行结果,并使用`.strip()`方法去除首尾的空格。

    使用`shell=True`参数可以执行shell命令;`capture_output=True`参数可以将标准输出和错误输出保存到`result.stdout`和`result.stderr`中;`text=True`参数可以将输出结果以文本形式返回。

    在示例代码中,我们分别执行了`ls`和`pwd`命令,并通过`print`函数打印出结果。

    你可以根据自己的需求,调用`run_command`函数执行任意的Linux命令,并获取命令执行结果。

    2年前 0条评论
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    在Python中执行Linux命令并返回结果可以使用以下方法:

    1. 使用os模块的system函数:
    “`python
    import os
    result = os.system(“command”)
    “`
    这种方法会直接执行命令,并返回命令的退出状态码。如果命令成功执行,返回值为0;如果命令执行失败,返回值为非零。

    2. 使用subprocess模块的check_output函数:
    “`python
    import subprocess
    result = subprocess.check_output(“command”, shell=True)
    “`
    这种方法会执行命令,并返回命令的输出结果。需要注意的是,该方法会将命令交给shell执行,因此需要加上shell=True参数。

    3. 使用subprocess模块的Popen函数:
    “`python
    import subprocess
    process = subprocess.Popen(“command”, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    output, error = process.communicate()
    “`
    这种方法与前一种方法类似,不同之处在于它可以分别获取命令的输出结果和错误信息。通过调用communicate方法可以获取输出结果和错误信息。

    4. 使用os模块的popen函数:
    “`python
    import os
    result_stream = os.popen(“command”)
    result = result_stream.read()
    “`
    这种方法会执行命令,并返回一个类似文件对象的流。通过调用read方法可以获取命令的输出结果。

    5. 使用paramikom模块的SSHClient类:
    “`python
    import paramiko
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname, port, username, password)
    stdin, stdout, stderr = ssh.exec_command(“command”)
    result = stdout.read()
    ssh.close()
    “`
    这种方法适用于通过SSH远程执行命令,并获取命令的输出结果。需要安装paramiko模块。通过SSHClient类的exec_command方法可以执行命令,并通过stdout属性获取输出结果。

    以上是几种常见的在Python中执行Linux命令并返回结果的方法,可以根据实际需求选择合适的方法使用。

    2年前 0条评论
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    在Python中执行Linux命令并返回结果需要使用`subprocess`模块。`subprocess`模块允许你在Python代码中调用系统命令,并获得其执行结果。

    下面是执行Linux命令并获取返回结果的一般步骤:

    1. 导入`subprocess`模块:
    “`python
    import subprocess
    “`

    2. 使用`subprocess.run()`函数执行命令并获取输出结果。`run()`函数会使用指定的命令行命令,并在新的子进程中执行。函数会等待命令执行完毕,然后返回一个`CompletedProcess`对象,其中包含命令的执行结果。
    “`python
    result = subprocess.run([“command”, “arg1”, “arg2”], capture_output=True, text=True)
    “`
    在上述代码中,`[“command”, “arg1”, “arg2”]`是要执行的Linux命令和参数。
    `capture_output=True`参数会将命令的输出结果捕获到`stdout`属性中。
    `text=True`参数会将输出结果作为字符串返回,而不是字节流。

    3. 获取执行结果:
    “`python
    output = result.stdout
    “`
    通过`stdout`属性获取命令的输出结果。

    4. 处理错误信息(可选):
    如果命令执行出错,可以通过检查`result.returncode`属性来获取错误码。
    “`python
    if result.returncode != 0:
    error_msg = result.stderr
    “`
    `returncode`等于0表示命令执行成功,非0值表示命令执行失败。

    以下是一个完整的示例代码:
    “`python
    import subprocess

    def execute_command(command):
    try:
    result = subprocess.run(command, capture_output=True, text=True)
    if result.returncode == 0:
    return result.stdout
    else:
    return result.stderr
    except Exception as e:
    return str(e)

    # 执行命令并获取输出结果
    output = execute_command([“ls”, “-l”])
    print(output)
    “`

    该示例代码执行`ls -l`命令,并将输出结果打印出来。

    需要注意的是,在使用`subprocess.run()`函数执行命令时,要谨慎处理用户输入的命令,以防止命令注入等安全问题。

    2年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部