python - 如何传递 python 脚本作为参数?

标签 python linux shell

我正在开发一个程序,我需要使用 4 个引脚号进行暴力破解才能获得标志。这是为了应对一些网络安全挑战。我能够创建一个 python 脚本来打印 4 位数字的每个组合。但是,我不知道如何将其作为参数传递给我需要暴力破解的程序。

我使用 Kali Linux 来运行程序并创建脚本。

#!/usr/bin/python
from itertools import product

numbers = '0123456789' # chars to look for

for length in range(1, 3): # only do lengths of 1 + 2
    to_attempt = product(chars, repeat=4)
    for attempt in to_attempt:
        print(''.join(attempt))

关于如何将这些结果作为参数传递给程序有什么想法吗?

最佳答案

您并不是尝试将 python 脚本作为参数传递,而是尝试将脚本的输出转换为另一个程序的参数。

有多种方法可以做到这一点。也许最有效的方法是从 Python 脚本调用程序而不是打印输出。

但是,如果您想迭代某个程序输出的行,可以按如下方式执行:

python script.py | while read -r; do
    ./program "$REPLY"  # or do whatever you want with "$REPLY"
done

如果您在循环中所做的只是运行单个程序,则也可以使用 xargs 来将行作为参数传递:

python script.py | xargs -n1 ./program

您可能根本不需要 Python 脚本。例如,如果您只想要所有四位数字的列表,您可以这样做

seq -w 0 9999

(-w 选项将 0 填充到 00001 填充到 0001 等)

xargs结合,给出

seq -w 0 9999 | xargs -n1 ./program

或者您可以在 shell 中编写整个循环:

for ((i=0; i<=9999; i++)); do
    # using printf to pad the number to 4 places
    ./program "$(printf '%04d' $i)"
done

关于python - 如何传递 python 脚本作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56471231/

相关文章:

python - PyQt:QFileDialog.getExistingDirectory 使用默认目录,用户独立

Python 是/否用户输入

python - 使用 Python 从列中获取唯一值

linux - 在 Bash 脚本中嵌入密码

Bash 如何删除或过滤带有值的参数?

python - 在Python中,使用其他变量的过去创建一个新列

linux - 在命令行上将字符串转换为十六进制

c++ - 编译 C++,Makefile 基础知识 : variables, 包括库

linux - 将 shell 脚本的输出重定向到程序

json - JQ : Parse specific output (get IP) from JSON file