0

So python has a convenient function as part of pwntools where one can sendline() to an executable. How can I emulate this functionality in bash?

Example

#whatever.py
x = input("First input please: ")
y = input("Second input please: ")

I know I can echo "input1" | python3 whatever.py to answer the first input, but I can't make it work multiline (echo "input1\ninput2" | ... doesn't work, and neither does echo "input1"; echo "input2" | ...).

belkarx
  • 345
  • 1
  • 10

1 Answers1

2

Your Python script requires two lines of input on its standard input stream. Any of the following would provide that:

  1. Two calls to echo in a subshell:

    ( echo 'line 1'; echo 'line 1' ) | python3 whatever.py
    
  2. Two calls to echo in a compound command:

    { echo 'line 1'; echo 'line 1'; } | python3 whatever.py
    
  3. A single call to echo using the non-standard -e option to interpret the embedded \n as a literal newline:

    echo -e 'line 1\nline 2' | python3 whatever.py
    
  4. One call to printf, formatting each subsequent argument as its own line of output. This would be more suitable for variable data than using echo, see Why is printf better than echo?.

    printf '%s\n' 'line 1' 'line 2' | python3 whatever.py
    
  5. Using a here-document redirection:

    python3 whatever.py <<'END_INPUT'
    line 1
    line 2
    END_INPUT
    
Kusalananda
  • 333,661