首页 > 解决方案 > Python argparse 削减了一半的论点

问题描述

我正在尝试输入 sha512 哈希作为参数,但 argparse 只是无缘无故地切断了它的一半。当我输入 Unix 哈希( MiqkFWCm1fNJI )时,它按预期工作。我试图搜索类似的东西,但没有找到任何东西。我的命令行参数代码如下所示:

def check_args():
    parse = argparse.ArgumentParser()
    parse.add_argument('-p', '--password', type=str, action='store', help='enter your hashed password: -p your_hash')
    parse.add_argument('-w', '--wordlist', help='add your wordlist: -w wordlist')
    parse.add_argument('-f', '--file', help='file with hashes: -f your_hashes')

    args_list = parse.parse_args()

    return args_list

使用它的部分代码:

    c_arg = check_args()

    psw = c_arg.password
    wordlist = c_arg.wordlist
    file = c_arg.file
    print(psw)

所以当我运行脚本时

python crack.py -p $6$krVh8s..$ttQmt30au3s9wHywp/KGdFKGe1WoEK4xpFJupMA.I06/tdv1//4x7e1gSU2e2Qu/1kQ0rfqXRxghfBX0Io1BJ.

我得到这个输出:

../KGdFKGe1WoEK4xpFJupMA.I06/tdv1//4x7e1gSU2e2Qu/1kQ0rfqXRxghfBX0Io1BJ.

应该是:

 $6$krVh8s..$ttQmt30au3s9wHywp/KGdFKGe1WoEK4xpFJupMA.I06/tdv1//4x7e1gSU2e2Qu/1kQ0rfqXRxghfBX0Io1BJ.

如果我使用这样的参数运行相同的脚本,它会按预期工作:

python crack.py -p MiqkFWCm1fNJI

输出:

MiqkFWCm1fNJI

这有什么问题,我怎样才能让 argparse 读取这种字符串?

标签: pythonpython-3.xcommand-line-argumentsargparse

解决方案


argparse您的问题与Python无关。

$6等是对 Unix/Linux 中 [不存在的] 环境变量的引用。它们的值为''(空字符串)。将整个散列括在单引号中,以保护数据不被 shell 解释:'$6$krVh8s..$ttQmt30au3s9wHywp/...'.


推荐阅读