How do I send input to an executable using python?

I have an executable C code binary which asks for 2 inputs. And I want to write a python code which gives the input automatically. What i mean is when i run 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. Here is image

I tried:

print(-1) 
print(something) 

a bit more brief is- i tried:

print(1)

print(2)

print(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:). However i want to do it using python code. 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.

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 :slight_smile:

Look at the documentation of print: it has some relevant keywords parameters. Otherwise you can check out sys.stdout.

I have an executable C code binary which asks for 2 inputs. And I want
to write a python code which gives the input automatically. What i mean
is when i run 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.

See the flush=True parameter to the print() funtion in the Python docs.

In Python, output streams are buffered. Very much like in C streams are
buffered if you’re using <stdio.h>.

By default, a stream to a terminal is line buffered and a stream to a
pipe or file is block buffered. So on a terminal you’d see print(“blah”)
immediately because it ends in a newline character, and that causes a
flush of the buffer, and therefore the data go to the terminal
immediately. But with a pipeline the data are only flushed if the output
buffer becomes full.

Therefore, if you’re sending data to another programme via a pipe and
reach a point where it is necessary for that programme to have seen
the data before proceeding, you must flush the output buffer.

You can include “flush=True” as a parameter to print() to do that at the
end of that print(), or you can call sys.stdout.flush() at the point
where you need it to happen.

Edit: I don’t have the exact C code, however i managed to reverse
engineer it and here is something which might help.

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

   printf("\nHow long is your secret: ");

There would normally be a flush(stdout) after this prompt, for the same
reasons you need that kind of thing in Python.

__isoc99_scanf(&DAT_0804a0dc,&secret_len);

Sounds like you read this from a programme trace, seeing the underlying
library call. The original source likely had a plain old “scanf()” call.

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

printf("\nEnter your secret: ");
getchar();

This getchar() seems a bit weird. Why read a single character here?

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

Cheers,
Cameron Simpson cs@cskk.id.au