subprocess

在Python里调用执行Linux或Windows系统的shell命令:system、popen、commands、subprocess

旧巷老猫 提交于 2020-01-27 14:14:59
Python 语言的灵活性和易用性,使得它非常适合作为“胶水”,去粘合各种系统命令(shell),完成复杂的运维工作和特殊场景的需求。本文主要介绍 Python 里如何调用执行 Linux 或 Windows 的系统命令(shell),方法主要有 system 、 popen 、 commands 和 subprocess 。 system os.system(cmd) ,这个方法使用起来最直接,是 同步 执行,会 阻塞 进程。所以,在需要返回结果的场景中,这个方法是最合适的。 但需要注意的是,其只返回执行得状态码(依赖于操作系统),其值为 int 类型, 0 表示 shell 执行成功, 256 则表示未找到命令。(状态码 Linux 和 Windows 是不一样的) 代码示例: import os status_code = os . system ( 'uptime' ) print ( 'status_code:' , status_code ) status_code : 0 popen os.popen(cmd [,mode [,bufsize]]) ,这个方法是以文件的形式返回 shell 命令执行后的结果,也是 同步 执行,会 阻塞 进程。 但与 os.system 的返回值不同, os.popen 通过 read() 或 readlines()

how to get the normal print statement execution when using stdout=subprocess.PIPE during subprocess call in python

好久不见. 提交于 2020-01-25 17:36:49
问题 I am starting a subprocess in python through this command: result = subprocess.Popen([sys.executable, "/subscript.py"] + parameter,stdout=subprocess.PIPE ) result.wait() out, err = result.communicate() for line in out.splitlines(): print line The problem that I am facing with this approach is that all the print statements in subscript.py are kind of buffered till the end and are printed on the terminal at the end of the execution. So if my subscript.py takes about 15 minutes in execution then

使用Python调用系统命令

[亡魂溺海] 提交于 2020-01-25 15:00:55
os.system() 该函数返回命令执行结果的返回值,system()函数在执行过程中进行了以下三步操作: 1、fork一个子进程; 2、在子进程中调用exec函数去执行命令; 3、在父进程中调用wait(阻塞)去等待子进程结束。 返回0表示命令执行成功,其他表示失败。 注意:使用该函数经常会莫名其妙地出现错误,但是直接执行命令并没有问题,所以一般建议不要使用。 用法:os.system("command") os.popen() 这种调用方式是通过管道的方式来实现,函数返回是 file read 的对象,对其进行读取read、readlines等操作可以看到执行的输出。 注意:如果命令执行失败,就读取不到内容。 用法:os.popen("command") subprocess.Popen() subprocess模块被推荐用来替换一些老的模块和函数,如:os.system、os.spawn*、os.popen*等 subprocess模块目的是fork一个新的进程并与之通信,最常用是定义类Popen,使用Popen可以创建进程,并与进程进行复杂的交互。其函数原型为: class subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn

subprocess, invoke C-program from within Python

大憨熊 提交于 2020-01-25 08:32:28
问题 I am trying to invoke a C-program, named “drule.c”, from within my Python-program “drulewrapper.py”. I am trying to use "subprocess" but cannot get it to work. 1) I compile “drule.c” on the Mac’s terminal and all works okay: $ gcc -o drule drule c $ ./drule D11 >P>Q>RQ Fyi, the input -- “D11” -- are axioms in predicate logic; the output -- “>P>Q>RQ” -- is the theorem that is proven and which I then want to process further in my Python program. 2) I write a short Python program (drulewrapper

How to download all files from s3 bucket to local linux server while passing bucket and local folder value at runtime using python

社会主义新天地 提交于 2020-01-25 08:30:13
问题 I am making script to download files form s3 bucket to local linux folder. To achieve that i have to use dynamic values for buckets and folders where we want to download stuff. I know how to do with aws s3 cp s3://bucket /linux/local/folder --recursive --p alusta But how to accept bucket value at runtime dwn_cmd = "aws s3 cp s3://bucket/name/" + str(year_name) + '/' + str(month_name) folder_path = "/local/linux/folder/" + folder_name #subprocess.call(['aws','s3','cp',dwn_cmd,folder_path,'-

Running vulture from a python script

随声附和 提交于 2020-01-25 06:59:44
问题 I'm trying to find a way to run vulture (which finds unused code in python projects) inside a python script. vulture documentation can be found here: https://pypi.org/project/vulture/ Does anyone know how to do it? The only way I know to use vulture is by shell commands. I tried to tun the shell commands from the script, using module subprocess, something like this: process = subprocess.run(['vulture', '.'], check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True)

Run a subprocess in python and both show the output in “real time” and save it to a variable

狂风中的少年 提交于 2020-01-25 00:27:10
问题 I would like to be able to run a subprocess from python code and both see the output in real time and once the process is finished have the output in a variable Right now I do one of either two things 1) Run subprocess using subprocess.call in that case I get the output in real time but I don't have at the end the output in a variable (I want to parse it and extract values from it) 2) Run subprocess using subprocess.check_output in that case I have the output in a variable but if I want to

【tool】远控免杀专题文章-msfvenom隐藏的参数

末鹿安然 提交于 2020-01-24 22:04:49
本节目录如下: msfvenom简介 msfvenom是msfpayload和msfencode的结合体,于2015年6月8日取代了msfpayload和msfencode。在此之后,metasploit-framework下面的的msfpayload(荷载生成器),msfencoder(编码器),msfcli(监听接口)都不再被支持。 常规参数 msfvenom所有参数 部分参数解读 -p, –payload < payload> 指定需要使用的payload(攻击荷载)。也可以使用自定义payload,几乎是支持全平台的 -l, –list [module_type] 列出指定模块的所有可用资源. 模块类型包括: payloads, encoders, nops, all -n, –nopsled < length> 为payload预先指定一个NOP滑动长度 -f, –format < format> 指定输出格式 (使用 –help-formats 来获取msf支持的输出格式列表) -e, –encoder [encoder] 指定需要使用的encoder(编码器),指定需要使用的编码,如果既没用-e选项也没用-b选项,则输出raw payload -a, –arch < architecture> 指定payload的目标架构,例如x86 | x64 | x86_64

p.stdout.read() doesn't work in my Python 3 codes

隐身守侯 提交于 2020-01-24 21:48:45
问题 I try to create a sub-process using the subprocess module in MAC OS. Below is my code: import subprocess p = subprocess.Popen("app", stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True) p.stdin.write(bytes("3\n", "ascii")) p.stdin.write(bytes("4\n", "ascii")) print(p.stdout.read()) The source code of app is : #include <iostream> using namespace std; int main() { int x, y; cout << "input x: " << endl; cin >> x; cout << "input y: " << endl; cin >> y; cout <

wxPython, capturing an output from subprocess in real-time

巧了我就是萌 提交于 2020-01-24 21:06:02
问题 I'm working on application in wxPython which is a GUI for a command line utility. In the GUI there is a text control which should display the output from the application. I'm launching the shell command using subprocess, but I don't get any output from it until it has completed. I have tried several solutions but none of them seems to work. Below is the code I'm using at the moment (updated): def onOk(self,event): self.getControl('infotxt').Clear() try: thread = threading.Thread(target=self