从这个STAF命令中解析Data
部分的最简单方法是什么?
找不到我可以传递给命令的STAF参数来自动执行此操作,
看起来像解析/正则表达式可能是最好的选择吗?
注意:我不想使用任何外部库。
[root@source ~]# STAF target PROCESS START SHELL COMMAND "ls" WAIT RETURNSTDOUT
Response
--------
{
Return Code: 0
Key : <None>
Files : [
{
Return Code: 0
Data : myFile.txt
myFile2.txt
myFile3.txt
}
]
}
相反,我希望将输出/结果格式化为..
[root@source ~]# STAF target PROCESS START SHELL COMMAND "ls" WAIT RETURNSTDOUT
myFile.txt
myFile2.txt
myFile3.txt
答案 0 :(得分:1)
Best way to this is Create a XML file and use python script to access the data part of STAFResult since STAF Return data in Marshalled form as "CONTENT" and python can be use to grab that.
I will try to explain it with simple example, Its an HTTP request to server.
<stafcmd>
<location>'%s'%machineName</location>
<service>'http'</service>
<request>'DOGET URL %s?phno=%s&shortCode=%s&query=%s' % (url, phno, shortCode, escapeQuery)</request>
</stafcmd>
<if expr="RC == 0">
<sequence>
<call function="'func_Script'"></call>
<if expr="rc == 0"> <!-- Pass At First Query -->
<sequence>
<message>'PASS@Fisrt HTTPRequest: Keyword = %s,\nRequired Response = %s,\ncontent=%s' %(query, response, content)</message>
<tcstatus result="'pass'">'Pass:' </tcstatus>
</sequence>
<else> <!-- Check For MORE -->
<call function="'Validate_QueryMore'"> </call>
</else>
</if>
</sequence>
<else>
<message>'ERROR: HTTPRequest QUERY : RC = %s Result= %s' %(rc,STAFResult)</message>
</else>
</if>
<function name="func_Script">
<script>
import re
content = STAFResult['content'].lower()
response = response.lower()
test = content.find(response)
if test != -1:
rc = 0
else:
rc = 1
</script>
</function>
Hope It will give you some Help.
答案 1 :(得分:0)
您可以通过sed脚本管道输出命令,该脚本将仅过滤掉您的文件名。这是第一次削减:
sed -ne '/^[a-z]/p;/Data/s/[^:]*: \(.*\)/\1/p'
这个想法是:如果一行以小写字母开头,那就是文件名(表达式直到第一个分号)。如果字符串“Data”在行上,则取出该行中第一个冒号后面的所有内容(分号后面的表达式)。其他一切都被忽略了。
您可能希望更具体而不仅仅是期望开头的小写字母(这会在开头过滤掉“响应”行,但如果您的文件名可能以大写字母开头,则赢了不行。另外,只是查找字符串“Data”可能有点过于笼统 - 该字符串也可能出现在文件名中。但希望你能得到这个想法。要使用它,请运行以下命令:
STAF ... | sed -ne ...