简体   繁体   English

使用 python 将输入发送到可执行文件

[英]Send input to executable using python

I have an executable C code binary which asks for 2 inputs.我有一个可执行文件 C 二进制代码,它要求 2 个输入。 And I want to write a python code which gives the input automatically.我想写一个 python 自动给出输入的代码。 What i mean is when i run python code.py |./ones-and-zeroes .我的意思是当我运行python code.py |./ones-and-zeroes It give inputs to the executable.它为可执行文件提供输入。 I tried using print(INPUT) but it only works if the input asked in next line, not in same line.我尝试使用print(INPUT)但只有当输入在下一行而不是在同一行时才有效。 Here is image这是图片

As you can see in image, I want to give -1 as first input, and something in second input.正如您在图像中看到的,我想将 -1 作为第一个输入,在第二个输入中给出一些内容。

I tried:我试过:

print(-1)
print(something)

a bit more brief is- i wrote:更简短的是-我写道:
print(1)打印(1)
print(2)打印(2)
print(3)打印(3)

it worked here because the input asked in new line它在这里工作是因为输入在新行中询问
but it did not work for other executable because input is asking input in the same line.但它不适用于其他可执行文件,因为输入要求在同一行输入。

i may not be clear here, sorry.我在这里可能不清楚,抱歉。 I simply want to input: -1 in the first question(How long is your secret:) and input: test or some string in second question(Enter your secret:).我只想在第一个问题中输入:-1(你的秘密有多长:)并在第二个问题中输入:测试或一些字符串(输入你的秘密:)。 However i want to do it using python code.但是我想使用 python 代码来完成。 not manually不是手动

Edit: I don't have the exact C code, however i managed to reverse engineer it and here is something which might help.编辑:我没有确切的 C 代码,但是我设法对其进行了逆向工程,这可能会有所帮助。

void getFlag(void)
{
    ushort secret_len;
    char your_secret[1936];

    printf("\nHow long is your secret: ");
    __isoc99_scanf(&DAT_0804a0dc,&secret_len);

    if(1927 < (short)secret_len) {
        puts("\nYour Secret is too long");
         exit(0);
    }

    printf("\nEnter your secret: ");
    getchar();
    fgets(your_secret, (uint)secret_len, stdin);
    puts("\nThe Enclave will shield your secrets!\n");
    return;
}

Thanks everyone for help:)谢谢大家的帮助:)

If you know all of the information you want to print on a single line there are a variety of ways to format up the multiple values on a single line of output. One option to get -1 and then the contents of the something variable on a single line would be to use "f-strings"如果您知道要在一行上打印的所有信息,则有多种方法可以在 output 的一行上设置多个值的格式。一种方法是获取 -1,然后获取 -1 上的 something 变量的内容单行将使用“f-strings”

print(f'-1 {something}')

Basically you can put any python code into curly braces and its string representation will be substituted into the string, and then the entire string will be printed with a newline at the end.基本上你可以将任何 python 代码放入花括号中,它的字符串表示将被替换到字符串中,然后整个字符串将在末尾打印一个换行符。

If you want to do multiple prints to the same line because you don't know all of your output at once, you need to use sys.stdout.write and then include an explicit '\n' when you really do need to write the newline.如果你想对同一行进行多次打印,因为你不知道你所有的 output 一次,你需要使用 sys.stdout.write 然后在你确实需要写的时候包含一个明确的'\n'新队。

I did not understand your problem well, but if you mean to use input and take a value in the same line, you can use this code我不太明白你的问题,但如果你的意思是在同一行中使用输入和取值,你可以使用这段代码

x=int(input("How long is your secret:")
y=input("Enter your secret:")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM