Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate two consecutive ENTER key presses for a command in a bash script?

Tags:

linux

bash

[EDITED] It can be considered as an extension to [this question][1]. echo | command The above command can be used to supply one 'ENTER' character to the command's first input request.

How can i supply the next 'ENTER' character to the same command in its second input request.

Please comment if any other details are required.

Am giving the specific example which i want to implement. I need to run SSH-keyGen commmand in my shell script. It will ask for following inputs:

  1. Enter the target file name
  2. Enter the pass phrase
  3. Enter the pass phrase again

How can we pass these three inputs to the command?

I tried with,

echo -ne "\n \n"| ssh-keygen  //which is passing two new lines for the first input request only.

and echo -ne "\n"|(echo -ne "\n"|ssh-keygen)// but still no positive result

Note: Am avoiding the input file name request in the above two command, just to make the things simple

like image 385
pruthvi Avatar asked Jun 08 '15 14:06

pruthvi


People also ask

How do you write a multi line command in a shell script?

Using a Backslash. The backslash (\) is an escape character that instructs the shell not to interpret the next character. If the next character is a newline, the shell will read the statement as not having reached its end. This allows a statement to span multiple lines.

How do I run a command multiple times in bash?

How To Run a Command Multiple Times in Bash. Wrap your statement for i in {1..n}; do someCommand; done , where n is a positive number and someCommand is any command. To access the variable (I use i but you can name it differently), you need to wrap it like this: ${i} . Execute the statement by pressing the Enter key.


1 Answers

You should take a look at expect:

#!/usr/bin/expect
#set timeout 10
set clientName [lindex $argv 0];
set hostName [lindex $argv 1];
set passWord [lindex $argv 2];

spawn ssh "$hostName";
expect "Password:";
send "$passWord\r";
expect "$hostName";
like image 145
sbriet Avatar answered Oct 18 '22 08:10

sbriet