我正在编写一个Python程序,需要返回在我的一个漏洞扫描中扫描的活动主机.我在返回XML之前使用过这种方法,但是当我尝试使用cut和grep这些额外的程序时,我遇到了问题.也许它不喜欢"管道"| 或者我可能在这里使用逗号做了一些完全错误但是我已经尝试了各种各样的东西,并且似乎无法让它返回结果,就像我从命令行独立运行命令一样.非常感谢您提供的任何帮助.
def activeHostsQuery():
args = ['curl', '-s', '-k', '-H', 'X-Requested-With: curl demoapp', '-u','username:password', 'https://qualysapi.qualys.com/api/2.0/fo/scan/?action=fetch&scan_ref=scan/1111111.22222&mode=brief&output_format=csv', '|', 'cut', '-d', '-f1', '|', 'sort', '|', 'uniq', '|', 'grep', '-E', '"\"[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\""', '|', 'wc', '-l']
activeHostsNumber = subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0]
return activeHostsNumber
Run Code Online (Sandbox Code Playgroud) 似乎我从未真正设法绕过子进程.这个命令行在bash中工作
avconv -i "concat:/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment0.ts|/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment1.ts" -vcodec copy -acodec copy /tmp/test1.ts
Run Code Online (Sandbox Code Playgroud)
如果我试试这个:
cmdline = ['avconv', '-i "concat:/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment0.ts|/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment1.ts"', '-vcodec copy', '-acodec copy', '/tmp/test1.ts']
subprocess.call(cmdline)
Run Code Online (Sandbox Code Playgroud)
它退出avconv时出现以下错误:
avconv version 0.8.3-4:0.8.3-0ubuntu0.12.04.1, Copyright (c) 2000-2012 the Libav developers
built on Jun 12 2012 16:52:09 with gcc 4.6.3
Unrecognized option 'i "concat:/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment0.ts|/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment1.ts"'
Failed to set value '-vcodec copy' for option 'i "concat:/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment0.ts|/tmp/BABAR_AND_THE_A-011A-hts-a-v1-cc3651d01841d748_Layer6/6148_Period1/segment1.ts"'
1
Run Code Online (Sandbox Code Playgroud)
我尝试过一些变种(包括shell = True),但我无法弄清楚问题.
Cjb做出了真正有效的答案.但我的真实代码更复杂.我相信很难从中得到一些东西.但我把它丢进去,以防万一我错过了一个obvoius问题.
def run_avconv(output_dir, fnames):
full_fnames = [os.path.join(output_dir, fname.replace('\n', ''))
for fname in fnames]
concatted_files = '|'.join(full_fnames)
cmd_line = [AVCONV_CMD,
'-i',
'"concat:' …Run Code Online (Sandbox Code Playgroud) Welp,我需要从python中删除一些巨大的临时目录,我似乎无法使用rm -r.我正在想一个大数据集(在s3上)我没有磁盘空间让他们离开.
通常我会从python调用命令的方法是
import subprocess
subprocess.call('rm','-r','/home/nathan/emptytest')
Run Code Online (Sandbox Code Playgroud)
这给了我
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 629, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?
成功:
>>> scp_cmd = r"sudo scp -i /home/backup/.ssh/id_rsa /opt/backups/*conf backup@a-hostname.local:/opt/backups/"
>>> subprocess.call(scp_cmd, shell=True)
1eadmin1.conf 100% 83KB 83.5KB/s 00:00
1stflr_1.conf 100% 2904 2.8KB/s 00:00
>>> scp_cmd = """sudo scp -i /home/backup/.ssh/id_rsa /opt/backups/*conf backup@a-hostname.local:/opt/backups/"""
>>> os.system(scp_cmd)
1eadmin1.conf 100% 83KB 87.3KB/s 00:00
1stflr_1.conf 100% 2904 3.4KB/s 00:00
Run Code Online (Sandbox Code Playgroud)
失败:
>>> scp_cmd = r"""sudo scp -i /home/backup/.ssh/id_rsa /opt/backups/*conf backup@a-hostname.local:/opt/backups/"""
>>> subprocess.call(scp_cmd, shell=True)
/opt/backups/*conf: No such file or directory
1
>>> subprocess.call(scp_cmd.split(' '))
/opt/backups/\*conf: No such file or directory
1
>>>
>>> subprocess.call(shlex.split(scp_cmd))
/opt/backups/*conf: No such …Run Code Online (Sandbox Code Playgroud) 我有一个工具,它返回一些关于我正在运行的机器的信息.在正常的命令行上它会是这样的 -
sudo/path-to-tool-directory/tool arg
这工作正常.现在我打破了这个并将其包含在我的python脚本中
result = subprocess.call(["sudo/path-to-tool-directory/tool","arg"])
它给我一个错误
在XYZ行的
subprocess.py ,在_execute_child中
引发child_exception
OSError:[Errno 2]没有这样的文件或目录
任何线索可能会出错?
import subprocess
import sys
proc = subprocess.Popen(["program.exe"], stdin=subprocess.PIPE) #the cmd program opens
proc.communicate(input="filename.txt") #here the filename should be entered (runs)
#then the program asks to enter a number:
proc.communicate(input="1") #(the cmd stops here and nothing is passed)
proc.communicate(input="2") # (same not passing anything)
Run Code Online (Sandbox Code Playgroud)
我如何使用python传递和与cmd通信.
谢谢.(使用Windows平台)
所以我有:
result = subprocess.check_output(['wine',
os.getcwd()+'/static/sigcheck.exe',
'-a','-i','-q',
self.tmpfile.path()])
Run Code Online (Sandbox Code Playgroud)
但每当我运行这个我得到这个错误
CalledProcessError: Command '['wine', '/home/static/sigcheck.exe', '-a', '-i', '-q', '/tmp/tmpxnsN5j']' returned non-zero exit status 1
Run Code Online (Sandbox Code Playgroud)
但如果我check_output改为call它工作正常:
Z:\tmp\tmpvOybcm:
Verified: Unsigned
File date: 9:08 AM 10/24/2012
Publisher: Hardcore Computer
Description: Farthest Emitters Converter
Product: Farthest Emitters Converter
Version: 3.2.0
File version: 3.2.0
fixme:mscoree:StrongNameSignatureVerificationEx (L"Z:\\tmp\\tmpvOybcm", 1, 0x33ec13): stub
Strong Name: Unsigned
Original Name: n/a
Internal Name: Farthest Emitters Converter
Copyright: Hardcore Computer 2006
Comments: n/a
Run Code Online (Sandbox Code Playgroud)
有什么理由check_output不起作用?
我正在将一个Windows应用程序移植到OS X 10.6.8.对我来说这是一个新平台,我面临一些困难.
该应用程序是一个小型的Web服务器(瓶子+服务员),由于子进程调用,它启动了一个浏览器(基于chrome嵌入式框架).
浏览器是一个应用程序文件,从gui启动时运行正常.
我这样推出它:
subprocess.Popen([os.getcwd()+"/cef/cefclient.app", '--url=http://127.0.0.1:8100'])
Run Code Online (Sandbox Code Playgroud)
不幸的是,这失败了OSError: permission denied.
我尝试使用sudo具有类似结果的脚本运行.
我可以使用以下命令从shell启动应用程序:
open -a "cef/cefclient.app" --args --url-http://127.0.0.1:8100
Run Code Online (Sandbox Code Playgroud)
但
subprocess.Popen(['open', '-a', os.getcwd()+'/cef/cefclient.app', '--args', '--url-http://127.0.0.1:8100'])
Run Code Online (Sandbox Code Playgroud)
失败,出现以下错误
FSPathMakeRef(/Users/.../cefclient.app) failed with error -43.
Run Code Online (Sandbox Code Playgroud)
知道如何解决这个问题吗?
我只想在我的覆盆子pi上建立一个小蟒蛇音乐客户端.我安装了"mpg321",它运行良好,但现在我的问题.发送命令后
os.system("mpg321 -R testPlayer")
Run Code Online (Sandbox Code Playgroud)
python等待播放,暂停或退出等用户输入.如果我在终端中写这个,播放器暂停音乐或退出.完美,但我希望python这样做,所以我发送命令
os.system("LOAD test.mp3")
Run Code Online (Sandbox Code Playgroud)
其中LOAD是加载此mp3的命令.但没有任何反应.当我通过终端退出播放器时,我收到错误消息:
sh: 1: LOAD: not found
Run Code Online (Sandbox Code Playgroud)
我认为这意味着
os.system("mpg321 -R testPlayer")
Run Code Online (Sandbox Code Playgroud)
采取整个过程,在我退出之后,python尝试执行comman LOAD.那么我如何让这些东西一起工作呢?
我的代码:
import os
class PyMusic:
def __init__(self):
print "initial stuff later"
def playFile(self, fileName, directory = ""):
os.system("mpg321 -R testPlayer")
os.system("LOAD test.mp3")
if __name__ == "__main__":
pymusic = PyMusic()
pymusic.playFile("test.mp3")
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
可能重复:
Python中的反引号等价物
我正在寻找在Python中运行终端命令(ls -l)的最佳方法.我已经阅读了有关子流程的内容,但我完全不了解它,如果有人可以尝试让我了解正在发生的事情,我将不胜感激.我需要使用ls -l命令检索一个硬链接号,即!= 1,然后保存此号码以匹配其他地方的目录号码.现在我想知道如何获取硬链接号并使用子进程将其保存到变量(如果有的话,可以使用更好的方法).
这是我到目前为止使用的代码:#!/ usr/bin/python
#tool that resolves time machine directories
import os
#create output file
os.chdir("/home/sean/Desktop")
hard_link_number = open('hardLinkNumber.log', 'w')
#move into mounted backup (figure out how to remove xe2 etc)
os.chdir("/mnt/Backups.backupdb/stuart dent\xe2\x80\x99s MacBook Pro/2010-08-10-160859/MAc")
#find hard link data
print>>hard_link_number, os.system("ls -la")
hard_link_number.close()
Run Code Online (Sandbox Code Playgroud)
os.system("ls -la")输出我需要的信息,但不会将其保存到我创建的文件中.我在别处读到os.system不会输出数据.
python ×10
subprocess ×10
os.system ×2
shell ×2
cmd ×1
communicate ×1
exception ×1
ffmpeg ×1
hardlink ×1
linux ×1
macos ×1
python-2.7 ×1
python-3.x ×1
rm ×1
windows ×1