python 中调用shell命令
subprocess模块
根据Python官方文档说明,subprocess模块用于取代上面这些模块。有一个用Python实现的并行ssh工具—mssh,代码很简短,不过很有意思,它在线程中调用subprocess启动子进程来干活。
- >>> from subprocess import call
- >>> call(["ls", "-l"])
subprocess与system相比的优势是它更灵活(你可以得到标准输出,标准错误,“真正”的状态代码,更好的错误处理,等..)。我认为使用os.system已过时,或即将过时。
#!/usr/bin/env python
#encoding=utf-8 import os
from subprocess import call
call(["ls","-l"]) path=os.getenv("phone_path")
print os.getenv("phone_path")
call(["adb","root"])
call(["adb","pull","/system/lib64/libteec.so",path+"/ree/client/libteec.so_64"])
可以执行获取环境变量,
执行adb pull命令。
太厉害了。
#!/usr/bin/env python
#encoding=utf-8 import os
from subprocess import call #通过call调用shell命令
from hashlib import sha1 #计算sha1sum值 call(["ls","-l"]) #实现从jenkins环境变量获取值
path=os.getenv("phone_path")
print os.getenv("phone_path") #实现adb pull,环境变量直接引用不用加引号,用+即可
call(["adb","root"])
call(["adb","pull","/system/lib64/libteec.so",path+"/ree/client/libteec.so_64"]) #实现adb push
#call(["adb","remount"])
#call(["adb","push",path+"/ree/client/libteec.so_64","/system/lib64/libteec.soa"]) #实现adb shell
#call(["adb","root"])
#call(["adb","shell","chmod","-R","777","/system/lib/nutlet_armtz/"])
#call(["adb","shell","./system/lib/nutlet_armtz/nutlet_helloworld"]) #计算sha1sum值
def getSha1(filename): #计算sha1
sha1Obj = sha1()
with open(filename,'rb') as f:
sha1Obj.update(f.read())
return sha1Obj.hexdigest() print getSha1(path+"/ree/client/libteec.so_64") #下面的调用可以当做一个整体,类似于在终端输入命令,其中间仍然可以调用参数,参数在引号外面
call("sha1sum " + path + "/ree/client/libteec.so_64 > remote_sha1", shell=True)
call("sha1sum " + path + "/ree/client/libteec.so_64 >> remote_sha1", shell=True)
#call(["sha1sum",path+"/ree/client/libteec.so_64",">","remote_sha1"]) print 'ok'
python 中调用shell命令的更多相关文章
- 【python中调用shell命令使用PIPE】使用PIPE作为stdout出现假卡死的情况——将stdout重定向为输出到临时文件
在Python中,调用:subprocess.Popen(cmd, stdout = PIPE, stderr = PIPE, shell= true)的时候,如果调用的shell命令本身在执行之后会 ...
- Awk中调用shell命令
Awk中调用shell命令 需求 在awk中,有时候需要调用linux系统中命令,如计算字符串的MD5值,并保存下来. 方法参考 call a shell command from inside aw ...
- 【转载】如何在C语言中调用shell命令
转载自:http://blog.csdn.net/chdhust/article/details/7951576 如何在C语言中调用shell命令 在linux操作系统中,很多shell命令使用起来非 ...
- python中执行shell命令的几个方法小结(转载)
转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...
- 「Python」6种python中执行shell命令方法
用Python调用Shell命令有如下几种方式: 第一种: os.system("The command you want"). 这个调用相当直接,且是同步进行的,程序需要阻塞并等 ...
- Python中调用shell
1 简单调用shell命令 os.system(command) 在一个子shell中运行command命令, 并返回command命令执行完毕后的退出状态. 这实际上是使用C标准库函数system( ...
- [Python]在python中调用shell脚本,并传入参数-02python操作shell实例
首先创建2个shell脚本文件,测试用. test_shell_no_para.sh 运行时,不需要传递参数 test_shell_2_para.sh 运行时,需要传递2个参数 test_shell ...
- linux C中调用shell命令和运行shell脚本
1.system(执行shell 命令) 相关函数 fork,execve,waitpid,popen表头文件 #include<stdlib.h>定义函数 int system(cons ...
- python中执行shell命令的几个方法小结
原文 http://www.jb51.net/article/55327.htm 最近有个需求就是页面上执行shell命令,第一想到的就是os.system, os.system('cat /proc ...
随机推荐
- ActiveMQ in Action(3) - Persistence
关键字: activemq 2.3 Persistence2.3.1 AMQ Message Store AMQ Message Store是ActiveMQ5.0缺省的持久化存储.Messag ...
- digitalocean注册验证账户、激活账号教程
注册digitalocean账号很简单,使用优惠链接注册digitalocean还能赠送10美元余额,digitalocean vps是优秀的SSD VPS,最便宜的套餐只要5美元/月. 由于中国大陆 ...
- debian root 可以远程登陆
vim /etc/ssh/sshd_config FROM: PermitRootLogin without-password TO: PermitRootLogin yes
- memset用法总结
memset是计算机中C/C++语言函数.将s所指向的某一块内存中的前n个 字节的内容全部设置为ch指定的ASCII值, 第一个值为指定的内存地址,块的大小由第三个参数指定,这个函数通常为新申请的内存 ...
- Adobe flash CC 汉化破解方法
转载整理自:http://www.sdifenzhou.com/flashprofessionalcc.html 断网,解压下载的破解dmg文件得到这些文件 第一步,解压安装包,安装,并打开,不要输入 ...
- 在cmd模式下对mysql的操作语句
A.window下的语句 1.mysqld -install //安装mysql服务 2.mysqld -remove //卸载mysql服务 3.net start mysql //启动服务 ...
- HTML-input标签需设置的属性
<input placeholder="这里输入门店名称" autocomplete="off" id="" name="& ...
- stm32菜单按键的设计
有点懒.看注释吧 // k0,enter/enable;k3:esc/disable// k1,value+/menu+;k2:menu-/value-; #include "sysmenu ...
- PHP7编译错误:php编译undefined reference to `libiconv 错误
ext/gd/libgd/.libs/gdkanji.o: In function `do_convert’: /root/php-5.2.12/ext/gd/libgd/gdkanji.c:350: ...
- Hibernate中自带ID的generator的含义
increment:代理主键,适合于所有数据库,由hibernate维护主键自增,和底层数据库无关,但是不适合于2个或以上hibernate进程. identity:代理主键,适合于Mysql或ms ...