使用subprocess模块

import subprocess

# 防火墙服务存在关闭状态
child1 = subprocess.Popen(["systemctl status firewalld | grep Active"], stdout=subprocess.PIPE, shell=True)
print(child1.communicate())
#----执行结果------  
(b' Active: inactive (dead)\n', 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\n', None)

[Python]执行Linux命令的更多相关文章

  1. python执行linux命令的两种方法

    python执行linux命令有两种方法: 在此以Linux常用的ls命令为例: 方法一:使用os模块 1 2 3 shell# python >> import os >> ...

  2. 再见Xshell、Xftp!Python执行Linux命令、上传下载远程文件

    相信大家应该都接触过Linux操作系统(Ubuntu.Centos等),那么在使用的Linux操作系统需要使用一些远程ssh工具,尤其是公网服务器. 常用的ssh工具主要有:Xshell.MobaXt ...

  3. 使用python执行linux命令

    python版本是2.7.12 一.简单的获取linux命令的执行结果,比如:获取一个PID的进程树结构,linux命令是pstree -p pid,在python中有一个模块可以方便的获取.至于有时 ...

  4. 用python执行Linux命令

    例1:在python中包装ls命令 #!/usr/bin/env python #python wapper for the ls command import subprocess subproce ...

  5. Python 执行linux 命令

    # !/usr/bin/env python # -*- coding: utf-8 -* import os def main(): file_list = os.popen("ls&qu ...

  6. 解决python执行linux查看文件命令后,因为^@特殊符号导致部分内容不显示的问题

    问题现象 文件a的内容如下: 查看第3行到第5行的内容:sed -n '3,5p' /tmp/test/a 可见在命令行执行,是没有问题的. 在python中使用paramiko模块执行linux命令 ...

  7. Python实现Linux命令xxd -i功能

    目录 Python实现Linux命令xxd -i功能 声明 一. Linux xxd -i功能 二. xxd -i常见用途 三. 类xxd -i功能的Python实现 Python实现Linux命令x ...

  8. Python执行Linux系统命令方法

    Python执行Linux系统命令的4种方法 (1) os.system 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 复制代码代码如下: system(command) -> ...

  9. 使用Android平板编程,执行linux命令

    android有一些应用支持开发, AIDE 介绍http://www.wandoujia.com/apps/com.aide.ui https://play.google.com/store/app ...

随机推荐

  1. mysql索引创建和使用细节(二)

    上篇粗略记录当mysql字段类型是string,传入int类型参数后失效当问题. 现在测试下mysql字段是int类型,传参string类型会发生什么. 题外话,最近膝盖手术后还在家养伤中,只怪自己以 ...

  2. Mysql.新建数据库和用户

    //建立数据库 drop database if exists 你的db名; create database 你的db名 CHARACTER SET utf8 COLLATE utf8_general ...

  3. Flask登录认证

    login函数 @app.route('/login/', methods=['GET', 'POST']) def login(): if request.method == 'GET': retu ...

  4. xhemj资料

    Github https://github.com/xhemj Gitee码云 https://gitee.io/xhemj Cnblogs博客园 https://www.cnblogs.com/xh ...

  5. doT 这个模板 是怎么实现的?(1)

  6. Ubuntu学习之路2

    由于数据越来越多,学习的也稍微多了点,故将一步一步学习到的命令和快捷键继续到这篇博客. 1. 查看隐藏文件和文件夹 Ctrl+H 2. 打开搜索管理器 Alt+F2 3. 查看系统内存 free -h ...

  7. 返回数据中提取数据的方法(JSON数据取其中某一个值的方法)

    返回数据中提取数据的方法 比如下面的案例是,取店铺名称 接口返回数据如下: {"Code":0,"Msg":"ok","Data& ...

  8. ThreeJS 物理材质shader源码分析(顶点着色器)

    再此之前推荐一款GLTF物理材质在线编辑器https://tinygltf.xyz/ ThreeJS 物理材质shader源码分析(顶点着色器) Threejs将shader代码分为ShaderLib ...

  9. CSS-18-媒体查询

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. Intellij IDEA使用(一)项目模板类型

    概述 IDEA默认打开后,会出现欢迎页面,可以选择打开Project的方式. 对于新建的Project,一般会选择[Create New Project],对于界面中,众多的Project类型,有时候 ...