使用带有 psexec 的查找问题
Issue using find with psexec
我在获取 psexec 时遇到问题,发现在我的 python 函数中可以很好地协同工作。这是我要开始工作的功能:
def CountString(address, file, findstr):
cmd_line = r'psexec \{0} cmd /c find /c "{1}" "{2}"'.format(address, findstr, file)
print cmd_line
myP = subprocess.Popen(cmd_line, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
out = timeout(myP.communicate, (), {}, 30, (None, None))
return out
这是我如何测试函数的示例:
test = CountString(r'RemotePC', r'c:\directory\File to Count.txt', r'String to count')
print test
在我的第一个代码片段中,我添加了一个 print cmd_line
以验证 cmd 行看起来是否正确,并且据我所知它确实如此。如果我 运行 使用我的示例输入的函数 returns:
psexec \RemotePC cmd /c find /c "String to count" "c:\directory\File to Count.txt"
我什至可以将 print cmd_line
的输出复制并粘贴到 cmd window 中,它按预期工作:
C:\>psexec \RemotePC cmd /c find /c "String to count" "c:\directory\File to Count.txt"
PsExec v1.97 - Execute processes remotely
Copyright (C) 2001-2009 Mark Russinovich
Sysinternals - www.sysinternals.com
---------- c:\directory\File to Count.txt: 3600
cmd exited on RemotePC with error code 0.
C:\>
但由于某些原因,我无法将 python 函数设为 return 结果,如果我没有超时,它就会停在那里并挂起。
我还尝试更改 cmd_line
的语法以在 find /c "{1}" "{2}"
:
周围添加 ""
cmd_line = r'psexec \{0} cmd /c "find /c "{1}" "{2}""'.format(address, findstr, file)
print cmd_line
的输出同样来自 cmd 行本身,但不会始终如一地在 python 中工作。
我注意到的一件奇怪的事情是,这将 return 我想要的可能是 25 次尝试中的 1 次。但是当我 运行 它再次针对同一台远程 PC 和同一文件时,它失败了。
注意: 我不反对使用 find
之外的东西,如果有其他东西会 return 我正在尝试的字符串的计数在文件中查找。但是我确实需要使用 psexec
,或者允许我在 XP 机器上远程执行命令的东西,因为我试图从中计算字符串的文件非常大并且会 return结果超过几千。
我找到了解决方法!我仍在使用 find
,但现在我不再直接使用它,而是将 findstr
.
的结果通过管道传递给它
这是我的 cmd_line
现在的样子:
cmd_line = r'psexec \{0} cmd /c "findstr /c:"{1}" "{2}" | find /v /c """'.format(address, findstr, file)
我在获取 psexec 时遇到问题,发现在我的 python 函数中可以很好地协同工作。这是我要开始工作的功能:
def CountString(address, file, findstr):
cmd_line = r'psexec \{0} cmd /c find /c "{1}" "{2}"'.format(address, findstr, file)
print cmd_line
myP = subprocess.Popen(cmd_line, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
out = timeout(myP.communicate, (), {}, 30, (None, None))
return out
这是我如何测试函数的示例:
test = CountString(r'RemotePC', r'c:\directory\File to Count.txt', r'String to count')
print test
在我的第一个代码片段中,我添加了一个 print cmd_line
以验证 cmd 行看起来是否正确,并且据我所知它确实如此。如果我 运行 使用我的示例输入的函数 returns:
psexec \RemotePC cmd /c find /c "String to count" "c:\directory\File to Count.txt"
我什至可以将 print cmd_line
的输出复制并粘贴到 cmd window 中,它按预期工作:
C:\>psexec \RemotePC cmd /c find /c "String to count" "c:\directory\File to Count.txt"
PsExec v1.97 - Execute processes remotely
Copyright (C) 2001-2009 Mark Russinovich
Sysinternals - www.sysinternals.com
---------- c:\directory\File to Count.txt: 3600
cmd exited on RemotePC with error code 0.
C:\>
但由于某些原因,我无法将 python 函数设为 return 结果,如果我没有超时,它就会停在那里并挂起。
我还尝试更改 cmd_line
的语法以在 find /c "{1}" "{2}"
:
""
cmd_line = r'psexec \{0} cmd /c "find /c "{1}" "{2}""'.format(address, findstr, file)
print cmd_line
的输出同样来自 cmd 行本身,但不会始终如一地在 python 中工作。
我注意到的一件奇怪的事情是,这将 return 我想要的可能是 25 次尝试中的 1 次。但是当我 运行 它再次针对同一台远程 PC 和同一文件时,它失败了。
注意: 我不反对使用 find
之外的东西,如果有其他东西会 return 我正在尝试的字符串的计数在文件中查找。但是我确实需要使用 psexec
,或者允许我在 XP 机器上远程执行命令的东西,因为我试图从中计算字符串的文件非常大并且会 return结果超过几千。
我找到了解决方法!我仍在使用 find
,但现在我不再直接使用它,而是将 findstr
.
这是我的 cmd_line
现在的样子:
cmd_line = r'psexec \{0} cmd /c "findstr /c:"{1}" "{2}" | find /v /c """'.format(address, findstr, file)