绕过子进程命令的大小限制
Bypassing size limitation on a subprocess command
我需要你的帮助来解决我的问题。我想使用子进程启动一个 powershell 命令。然而,使用的命令会很长,所以它会大于缓冲区的大小。我做了很多测试,现在我找不到好的解决方案。
我想将我的大缓冲区(它是 base64 内容)拆分成小缓冲区,但我不知道如何使用子进程。
例如,这是我的子流程调用:
a = "a" * 32716
command = ["powershell.exe", "/c", "$base64='%s'\n" % a, "Write-Host $base64"]
process = subprocess.Popen(command, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, universal_newlines=True)
如果变量“a”的大小小于 32717 则有效,如果它更大(例如:等于 32717)我有以下错误:WindowsError: [错误 87] 参数不正确。如果它大很多,我有这个错误:WindowsError: [Error 206] 文件名或扩展名太长。
我试图将“command”变量拆分成小缓冲区,但缓冲区的内容不是按单元格划分的,它是整个选项卡的串联,所以它不会不要改变问题。
command = ["powershell.exe", "/c", "$base64=" + small_buffer1, "$base64=$base64+" small_buffer2, etc.]
另一个想法是使用不同的子进程调用。问题是我无法连接一个变量,因为它不会在同一个 powershell 实例上定义。所以在子进程之间,我会丢失“base64”的内容。
subprocess.Popen(["powershell.exe", "/c", "$base64='%s'\n" % a], stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
# $base64 will be empty
subprocess.Popen(["powershell.exe", "/c", "Write-Host $base64"], stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
如果有人有想法,那就太好了。我认为,可以定义一个 powershell 实例并将缓冲区作为交互式 shell 添加到其中。
非常感谢您的帮助。
我找到的解决方案很容易实施。目标是保持子进程打开并将缓冲区写入标准输入。
这是一个例子:
# keep the pipe open
p = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE, shell=True)
# initialize the powershell value
p.stdin.write("$data=\"\""+"\n")
# split the buffer to small buffers
size = 25000
tab = [veryLongLine[i:i+size] for i in range(0, len(veryLongLine), size)]
for t in tab:
# concatenate small buffers to obtain the "veryLongLine" value
p.stdin.write("$data+=\"%s\"\n" % t)
# flush the buffer every time (the $data value is not flushed)
p.stdin.flush()
# write the output
p.stdin.write("Write-Host $data\n")
希望对您有所帮助!
我需要你的帮助来解决我的问题。我想使用子进程启动一个 powershell 命令。然而,使用的命令会很长,所以它会大于缓冲区的大小。我做了很多测试,现在我找不到好的解决方案。 我想将我的大缓冲区(它是 base64 内容)拆分成小缓冲区,但我不知道如何使用子进程。
例如,这是我的子流程调用:
a = "a" * 32716
command = ["powershell.exe", "/c", "$base64='%s'\n" % a, "Write-Host $base64"]
process = subprocess.Popen(command, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, universal_newlines=True)
如果变量“a”的大小小于 32717 则有效,如果它更大(例如:等于 32717)我有以下错误:WindowsError: [错误 87] 参数不正确。如果它大很多,我有这个错误:WindowsError: [Error 206] 文件名或扩展名太长。
我试图将“command”变量拆分成小缓冲区,但缓冲区的内容不是按单元格划分的,它是整个选项卡的串联,所以它不会不要改变问题。
command = ["powershell.exe", "/c", "$base64=" + small_buffer1, "$base64=$base64+" small_buffer2, etc.]
另一个想法是使用不同的子进程调用。问题是我无法连接一个变量,因为它不会在同一个 powershell 实例上定义。所以在子进程之间,我会丢失“base64”的内容。
subprocess.Popen(["powershell.exe", "/c", "$base64='%s'\n" % a], stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
# $base64 will be empty
subprocess.Popen(["powershell.exe", "/c", "Write-Host $base64"], stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
如果有人有想法,那就太好了。我认为,可以定义一个 powershell 实例并将缓冲区作为交互式 shell 添加到其中。
非常感谢您的帮助。
我找到的解决方案很容易实施。目标是保持子进程打开并将缓冲区写入标准输入。 这是一个例子:
# keep the pipe open
p = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE, shell=True)
# initialize the powershell value
p.stdin.write("$data=\"\""+"\n")
# split the buffer to small buffers
size = 25000
tab = [veryLongLine[i:i+size] for i in range(0, len(veryLongLine), size)]
for t in tab:
# concatenate small buffers to obtain the "veryLongLine" value
p.stdin.write("$data+=\"%s\"\n" % t)
# flush the buffer every time (the $data value is not flushed)
p.stdin.flush()
# write the output
p.stdin.write("Write-Host $data\n")
希望对您有所帮助!