If I have a linux command that prompts for 2 or more input, how can these inputs be passed to the prompt by defining it in the command line? Is there something you can add behind the command to do this?
In the example below, how can you run the command and pass it the username and password without having to type it in when the system asks for them?
Example command that asks for username and password
git clone https://github.com/username/repo.git
Just an example, please don't suggest doing the git clone using ssh instead of http, or that its insecure to expose the password in the command
When at the MS-DOS, Windows command line or a Linux or Unix shell, typing in long file names or directories can become a burden. Use the Tab to autocomplete the names of directories and files while in the command line.
Method 1: Use redirection to save command output to file in Linux. You can use redirection in Linux for this purpose. With redirection operator, instead of showing the output on the screen, it goes to the provided file. The > redirects the command output to a file replacing any existing content on the file.
This is the most dependable shortcut in Linux to execute the last run command in the terminal. Just press the Ctrl and P keys together to fill the prompt with the last executed command and you are ready to go.
The "expect" package is built for this kind of thing. If you install it, check out "autoexpect".
Example:
autoexpect
# Output: autoexpect started, file is script.exp
# Login with user/pass with ssh - just to test
ssh -o PubkeyAuthentication=no myuser@myhost
<type-password>
<command>
<command>
exit
# Now exit once more to exit autoexpect
# Output: autoexpect done, file is script.exp
Next thing is to edit script.exp and clean it up. There is loads of documentation on this.
I want the bounty, and I can't close as dup because the answer is on a different SE.
Quoting the answer on superuser:
Use the program expect
which has been conceived for exactly your needs which were felt already by users in a land before our time, given that its man page states:
Connect to another network or BBS (e.g., MCI Mail, CompuServe).
Compuserve, was that before or after the French Revolution? Anyway.
spawn "git clone ssh://[email protected]/myproject.git"
expect "Enter passphrase for key..." { send "myPassword\r" }
read -p "enter..."
Perhaps the last line is superfluous.
Ceterum censeo: Don't do that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With