如何与 python 中的 cmd 提示符交互
How can I interact with the cmd prompt from python
我正在编写一个非常短的程序来检查文件安全的 MD5 和
我想获取用户的文件路径和命令“CertUtil -hashfile MD5”的校验和的预期输出,用户输入的文件路径在哪里,然后我想获取作为字符串的命令提示符输出,用于与用户指定的预期输出进行比较。这可能吗?如果是这样,我如何修改下面的代码以允许将文件路径作为变量提交并从命令提示符中获取输出?
我在 python 3.9.0 64 位 Windows 10 上编码,除非绝对必要,否则我想避免安装和额外的库
'''
#CheckSumChecker! By Joseph
import os
#User file input
data = input("Paste Filepath Here: ")
correct_sum = ("Paste the expected output here: ")
#File hash is checked using cmd "cmd /k "CertUtil -hashfile filepath MD5"
os.system('cmd /k "CertUtil -hashfile C:\Windows\lsasetup.log MD5"')
if correct_sum == cmd_output:
print("The sums match!" correct_sum "=" cmd_output)
else:
print("The sums don't match! Check that your inputs were correct" correct_sum "is not equal to" cmd_output)
'''
您可以使用 subprocess.check_output
。 (我已在此处修复了您的代码中的其他一些错误。)
import subprocess
input_path = input("Paste Filepath Here: ")
correct_sum = input("Paste the expected output here: ")
output = (
subprocess.check_output(
["CertUtil", "-hashfile", input_path, "MD5",]
)
.decode()
.strip()
)
print("Result:", output)
if correct_sum == output:
print("The sums match!", correct_sum, "=", cmd_output)
else:
print(
"The sums don't match! Check that your inputs were correct",
correct_sum,
"is not equal to",
cmd_output,
)
或者,要完全避免使用 CertUtil,请使用内置的 hashlib
模块。
您必须注意不要一次将整个文件读入内存...
import hashlib
input_path = input("Paste Filepath Here: ")
correct_sum = input("Paste the expected output here: ")
hasher = hashlib.md5()
with open(input_path, "rb") as f:
while True:
chunk = f.read(524288)
if not chunk:
break
hasher.update(chunk)
output = hasher.hexdigest()
print("Result:", output)
if correct_sum == output:
print("The sums match!", correct_sum, "=", cmd_output)
else:
print(
"The sums don't match! Check that your inputs were correct",
correct_sum,
"is not equal to",
cmd_output,
)
我正在编写一个非常短的程序来检查文件安全的 MD5 和
我想获取用户的文件路径和命令“CertUtil -hashfile MD5”的校验和的预期输出,用户输入的文件路径在哪里,然后我想获取作为字符串的命令提示符输出,用于与用户指定的预期输出进行比较。这可能吗?如果是这样,我如何修改下面的代码以允许将文件路径作为变量提交并从命令提示符中获取输出?
我在 python 3.9.0 64 位 Windows 10 上编码,除非绝对必要,否则我想避免安装和额外的库
'''
#CheckSumChecker! By Joseph
import os
#User file input
data = input("Paste Filepath Here: ")
correct_sum = ("Paste the expected output here: ")
#File hash is checked using cmd "cmd /k "CertUtil -hashfile filepath MD5"
os.system('cmd /k "CertUtil -hashfile C:\Windows\lsasetup.log MD5"')
if correct_sum == cmd_output:
print("The sums match!" correct_sum "=" cmd_output)
else:
print("The sums don't match! Check that your inputs were correct" correct_sum "is not equal to" cmd_output)
'''
您可以使用 subprocess.check_output
。 (我已在此处修复了您的代码中的其他一些错误。)
import subprocess
input_path = input("Paste Filepath Here: ")
correct_sum = input("Paste the expected output here: ")
output = (
subprocess.check_output(
["CertUtil", "-hashfile", input_path, "MD5",]
)
.decode()
.strip()
)
print("Result:", output)
if correct_sum == output:
print("The sums match!", correct_sum, "=", cmd_output)
else:
print(
"The sums don't match! Check that your inputs were correct",
correct_sum,
"is not equal to",
cmd_output,
)
或者,要完全避免使用 CertUtil,请使用内置的 hashlib
模块。
您必须注意不要一次将整个文件读入内存...
import hashlib
input_path = input("Paste Filepath Here: ")
correct_sum = input("Paste the expected output here: ")
hasher = hashlib.md5()
with open(input_path, "rb") as f:
while True:
chunk = f.read(524288)
if not chunk:
break
hasher.update(chunk)
output = hasher.hexdigest()
print("Result:", output)
if correct_sum == output:
print("The sums match!", correct_sum, "=", cmd_output)
else:
print(
"The sums don't match! Check that your inputs were correct",
correct_sum,
"is not equal to",
cmd_output,
)