python中执行shell命令

之前执行shell命令多是通过os.system(shell命令)的方式来执行,比较麻烦。

了解到sh是一个比subprocess好的库,能够执行shell命令

1.查看ip:

[root@iZj6cbstl2n6r280a27eppZ myblog]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.31.253.193 netmask 255.255.240.0 broadcast 172.31.255.255
ether 00:16:3e:01:72:ac txqueuelen 1000 (Ethernet)
RX packets 235322 bytes 231762359 (221.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 78402 bytes 15382388 (14.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

用sh完成:

In [13]: from sh import ifconfig

In [14]: print ifconfig("eth0")
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.31.253.193 netmask 255.255.240.0 broadcast 172.31.255.255
ether 00:16:3e:01:72:ac txqueuelen 1000 (Ethernet)
RX packets 236463 bytes 231849592 (221.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 79234 bytes 15505613 (14.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

2.查看/app目录下的文件:

In [24]: print sh.ls("/app")
git-2.14.1 myblog pyinit.py v2.14.1.tar.gz

In [27]: sh.ls("-l", "/app")
Out[27]:
drwxrwxr-x 26 root root 24576 9?. 12 20:52 git-2.14.1
drwxr-xr-x 6 root root 4096 9?. 13 21:53 myblog
-rw-r--r-- 1 root root 273 9?. 12 20:43 pyinit.py
-rw-r--r-- 1 root root 6983655 9?. 12 20:48 v2.14.1.tar.gz

查看/app下有多少个文件:

In [28]: sh.wc(sh.ls("-l", "/app"), "-l")
Out[28]: 5

3. 将/app下的文件存到列表里:

In [42]: list=sh.glob('/app/*')

In [43]: list
Out[43]: ['/app/git-2.14.1', '/app/v2.14.1.tar.gz', '/app/pyinit.py', '/app/myblog']

4.管道:

In [45]: print(sh.sort(sh.du(sh.glob('/app/*'),'-shc'),'-rn'))
397M ?.?
378M /app/git-2.14.1
13M /app/myblog
6.7M /app/v2.14.1.tar.gz
4.0K /app/pyinit.py

使用sh库执行shell命令的更多相关文章

  1. node.js在Linux下执行shell命令、.sh脚本

    首先,引入子进程模块 var process = require('child_process'); 执行shell命令 调用该模块暴露出来的方法exec process.exec('shutdown ...

  2. python之commands和subprocess入门介绍(可执行shell命令的模块)

    一.commands模块 1.介绍 当我们使用Python进行编码的时候,但是又想运行一些shell命令,去创建文件夹.移动文件等等操作时,我们可以使用一些Python库去执行shell命令. com ...

  3. Linux远程执行Shell命令或脚本

    ## 远程执行shell命令 ssh [user]@[server] '[command]' # eg. ssh root@192.168.1.1 'uptime' ## 远程执行本地shell脚本 ...

  4. Android Java执行Shell命令

    最新内容建议直接访问原文:http://www.trinea.cn/android/android-java-execute-shell-commands/ 主要介绍Android或Java应用中如何 ...

  5. nodejs 执行shell 命令

    有需要从前端操作服务器执行shell命令的需求 建立一个process.js文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 var process =  ...

  6. Android执行shell命令

    一.方法 /** * 执行一个shell命令,并返回字符串值 * * @param cmd * 命令名称&参数组成的数组(例如:{"/system/bin/cat", &q ...

  7. CMake如何执行shell命令

    我在cmake编译后想执行一些特定的shell命令(执行.lcov收集代码覆盖报告等),我又不想写到XX.sh的shell脚本中,如何直接通过CMake执行shell命令呢? 在网上翻江倒海了一下,找 ...

  8. 如何执行shell命令

    可使用 git 命令行来执行shell命令,如 D 盘下的一 shell 脚本 test.sh 如下: echo "Hello world" 打开命令行,输入命令执行: 转载请注明 ...

  9. python中执行shell命令的几个方法小结

    原文 http://www.jb51.net/article/55327.htm 最近有个需求就是页面上执行shell命令,第一想到的就是os.system, os.system('cat /proc ...

随机推荐

  1. LeetCode——Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中 ...

  2. [TypeScript] Asynchronous Iteration using for-await-of

    The for-await-of syntax is similar to the for-of iteration. The key difference is that it automatica ...

  3. ShopEx文章页添加上一篇下一篇功能

    在全部的文章页中,会常常发现都会有这么一个功能.能引导用户去查看上一篇文章或下一篇文章,而在ShopEx中,我DEZEND了一下文章模型.并没有找到上一篇这种函数功能,因此,这就须要我们手动在相应的文 ...

  4. MySQL 以及 Python 实现排名窗体函数

    大部分数据库都提供了窗体函数.比方RANK,ROW_NUMBER等等. MySQL 这方面没有直接提供.可是能够变相的实现.我曾经写了row_number 的实现,今天有时间把 rank 的实现贴出来 ...

  5. 85.Mongoose指南 - Schema

    转自:https://www.bbsmax.com/A/pRdBnKpPdn/ 定义schema 用mongoose的第一件事情就应该是定义schema. schema是什么呢? 它类似于关系数据库的 ...

  6. PostgreSQL Replication之第二章 理解PostgreSQL的事务日志(3)

    2.3 理解一致性和数据丢失 挖掘PostgreSQL事务日志而不考虑一致性是不可能的.在本章的第一部分,我们已经大体上解释了事务日志的基本思想.您已经知道,无需事先的日志改变的能力,使数据处于一种好 ...

  7. B-Boxes

    http://agc010.contest.atcoder.jp/tasks/agc010_b Problem Statement There are N boxes arranged in a ci ...

  8. UI Framework-1: Aura Event Handling

    Event Handling A diagram of the architecture of this system:     HWNDMessageHandler owns the WNDPROC ...

  9. CF85E Guard Towers(二分答案+二分图)

    题意 已知 N 座塔的坐标,N≤5000 把它们分成两组,使得同组内的两座塔的曼哈顿距离最大值最小 在此前提下求出有多少种分组方案 mod 109+7 题解 二分答案 mid 曼哈顿距离 >mi ...

  10. Laravel API 允许跨域访问

    服务器A请求服务器B的接口,那么一般会出现跨域问题.全解跨域请求处理办法 XMLHttpRequest cannot load http://api.console.vms3.com/api/user ...