zoukankan      html  css  js  c++  java
  • [Python]执行Linux命令

    使用subprocess模块

    import subprocess
    
    # 防火墙服务存在关闭状态
    child1 = subprocess.Popen(["systemctl status firewalld | grep Active"], stdout=subprocess.PIPE, shell=True)
    print(child1.communicate())
    #----执行结果------  
    (b
    ' Active: inactive (dead) ', None)
    # samba服务不存在
    child2 = subprocess.Popen(["systemctl status smbd | grep Active"], stdout=subprocess.PIPE, shell=True)
    
    print(child2.communicate())
    #----执行结果------ 
    Unit smbd.service could not be found.
    (b
    '', None)
    # network存在激活状态
    child3 = subprocess.Popen(["systemctl status network | grep Active"], stdout=subprocess.PIPE, shell=True)
    
    print(child3.communicate())
    
    #----执行结果------
    
    (b'   Active: active (exited) since Tue 2020-02-11 20:19:08 CST; 21h ago
    ', None)
  • 相关阅读:
    ES进阶--01
    JVM--02
    JVM--01
    ES--08
    ES--07
    ES--06
    python实现当前主机ip 主机名称的获取
    djang中的blank=True 和null = True的区别
    python中yield的用法详解
    python 编写古诗赤壁赋
  • 原文地址:https://www.cnblogs.com/leoshi/p/12299925.html
Copyright © 2011-2022 走看看