elixir elixir - 8 months ago 26
Bash Question

Python print redirect as stdin command line argument

I have a binary foo which requires two command line arguments: username and password.

I have written script.py to generate the username and password. Currently, I am using print to print them to stdout and then manually copy and paste them in the shell when I call foo in shell, i.e.,

$python script.py
username password
(i copied and paste the output below)
$./foo username password

However, I need to generate special bytes which are not printable in stdout and therefore if I copy and paste from stdout, these special byte values are gone. How can I redirect my python output as the argument for foo?

BTW: I have tried using call in subprocess to directly call foo in python, this is not ideal because if I trigger a seg fault in the foo, it does not reflected in bash.



./foo $(python script.py)

To demonstrates that this works and provides foo with two arguments, let's use this script.py:

$ cat script.py
print("name1 pass1")

And let's use this foo so that we can see what arguments were provided to it:

$ cat foo
echo "1=$1 2=$2"

Here is the result of execution:

$ ./foo $(python script.py)
1=name1 2=pass1

As you can see, foo received the name as its first argument and the password as its second argument.

Security Note: The OP has stated that this is not relevant for his application but, for others who may read this with other applications in mind, be aware that passing a password on a command line is not secure: full command lines are readily available to all users on a system.