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 ...
随机推荐
- MVC3+EF4.1学习系列(九)-----EF4.1其他的一些技巧的使用
上节通过一系列重构 简单的项目就实现了 不过还有些EF的功能没有讲 这节就通过项目 讲讲EF其他的功能与技巧 一.直接执行SQL语句 通常来讲 EF 不用写SQL语句的 但是 在有些场合 比如对生 ...
- PHP中使用CURL(五)
curl伪造IP和来源 client.php请求server.php client.php <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_U ...
- Andorid第三方库
博客: http://blog.csdn.net/codywangziham01/article/details/11818559#t8 RoboGuice: http://www.importnew ...
- 放弃阿里云主机,选择高性价比Vultr VPS免备案
阿里云主机ECS推广多年后,质量有所改善,但我依然强烈不推荐阿里云主机.考虑性价比带宽速度等因素后,我推荐的vps品牌有vultr和digitalocean,还有大名鼎鼎的linode,是中国用户的最 ...
- 去除VisualStudio中拼写错误检测的红色波浪线
去除VisualStudio中拼写错误检测的红色波浪线 在Visual Assistant中将 Underline spelling errors in comments and strings us ...
- JS 经典代码段总结 start from 2016-08-22
1.for(var i = 0, max = myArray.length; i < max ; i++){ //用myArrayy[i]来做点什么 } 用max存储myArray的长度,防止每 ...
- 【第四篇】androidEventbus源代码阅读和分析
1,分析androidEventbus的注册源代码: 我们在使用androidEventbus的第一步是注册eventbus,如下代码: EventBus.getDefault().register( ...
- SCU 1069 POJ 2955 Brackets
区间DP #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- ios UIApplication简单使用
每个app有且只有一个UIApplication对象,当程序启动的时候通过调用UIApplicationMain方法得到的.可以通过sharedApplication方法得到. UIApplicati ...
- Linux使用期间命令积累
1.调出终端 Ctrl+Alt+t 2.sudo是linux系统管理指令,是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,如halt,reboot,su等等. sudo apt-g ...