There is a situation when I try to call a command with parameters, after which I have to enter some more characters from the keyboard. For example:

1 c:\> gpg -d wtf.crypt 2 Enter passphrase: 

When we work on the command line, it's not very bad. You can simply enter what is expected of us.

But in my task I want to work through php, for example. system () command.

Then it turns out, after performing the command given in the example above, the page will freeze, and somewhere in the background I will be expected to enter something.

The question is whether there is a system in windows and linux that allows you to pre-set the parameters that can be used if there are questions after calling the command?

Fictional example: gpg -d wtf.crypt && [qwerty, y, y, 1,1,3] in square brackets, deferred parameters, in order of appearance ... is there something like that?

  • 3
    there is an expect command in Linux - it actually does what it needs, only it is a bit heaped up. She can read lines from the command and correctly respond to them. - KoVadim
  • Thank you for the tip. - silksofthesoul

2 answers 2

It is done through pipe (|) Here is an example of entering a password when testing an archive:

 echo infected|7z.exe t arc.zip 

Actually, echo displays the desired text, which through | transferred to 7z.exe. For GPG, you may need to use the --passphrase-fd parameter.

  • Thank. I will try. - silksofthesoul

This is called standard input redirection. There are a lot of options in Linux. Bash understands:

Redirect from file:

  cmd < file 

Connecting the output of one program to another input (pipe or pipe):

 cmd1 | cmd2 

Entering the program data recorded further in the following lines after the command (Here Document):

 cmd <<EOF исходные данные EOF 

Entering One String (Here String):

 cmd <<<'Исходные данные' 

In all cases, if the program needs more data than you provide it, it will "rest" at the end of the file, it will not be able to read anymore.

Programs requesting passwords (for example, ssh) often make it so that they themselves access the terminal from where they were running, reading data, ignoring standard input. Here input redirection will not help, it is done on purpose to eliminate the possibility of password interception. Then they usually have alternative safer ways to set a password.