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 ...
随机推荐
- .Net Core 中使用Session
1.在 Startup 中 ConfigureServices 添加Session public void ConfigureServices(IServiceCollection services) ...
- 使用rdesktop远程连接Windows桌面
之前使用的是KDE下的krdc.该程序的Grab Keys功能存在bug,导致Alt+TAB大多数时候不能被捕捉,从而无法使用键盘切换窗口.不过,其全屏功能是正常的,在多显示器的情况下,全屏只在一个屏 ...
- windows任务计划程序路径设置
用任务计划启动程序,特别是脚本,比如我要启动python脚本,其中有一句是这么写的 BasePath = removeLastSlash(os.path.abspath("..\\..\\& ...
- 搭建localhost的目录环境
.打开系统盘,默认是C:\Windows\System32\drivers\etc,如果系统盘是D盘就打开D:\Windows\System32\drivers\etc: .用记事本打开hosts: ...
- stm32菜单按键的设计
有点懒.看注释吧 // k0,enter/enable;k3:esc/disable// k1,value+/menu+;k2:menu-/value-; #include "sysmenu ...
- javascript 深入浅出 (未完成4-17)
慕课网javascript总结 课程地址 课程大纲: 一.数据类型 二.表达式和运算符 三.语句 四.对象 五.数组 六.函数 七.this 八.闭包和作用域 九.OOP 十.正则与模式匹配 ---- ...
- Webdriver如何解决页面元素过期:org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
当运行Webdriver时抛出如下异常:org.openqa.selenium.StaleElementReferenceException: Element not found in the cac ...
- java URL编程
一.URL编程技术 URL是统一资源定位器的缩写,它是指向互联网“资源”的指针.URL表示了Internet上某个资源的地址.URL支持http,file,ftp等 多种协议.通过URL标识,可以直接 ...
- android mediaplayer
- (响应式PC端媒体查询)电脑屏幕分辨率尺寸大全
(响应式PC端媒体查询)电脑屏幕分辨率尺寸大全 时间:2015-08-17 16:50:40 阅读:3961 评论:0 收藏:0 [点我收藏+] 标签:styl ...