Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically feed input to Linux command line [duplicate]

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

like image 465
Nyxynyx Avatar asked Apr 16 '13 00:04

Nyxynyx


People also ask

How do I autofill commands in Linux?

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.

How do you copy the output of a command to a file in Linux?

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.

What is the command to repeat the command you just executed?

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.


2 Answers

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.

like image 122
sastorsl Avatar answered Sep 28 '22 05:09

sastorsl


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.

like image 31
Peter - Reinstate Monica Avatar answered Sep 28 '22 04:09

Peter - Reinstate Monica