Reputation: 124
I am using expect script and some shell script command as below.
#!/usr/bin/expect
#!/bin/bash
spawn sudo mkdir -p /usr/lib/jvm1
expect "\[sudo\] password for hduser:"
send "cisco\r"
spawn sudo apt-get install openssh-client
But the above command didnot install openssh-client. It just shows up the command. and nothing is installed.
Where I am doing wrong here ?
Upvotes: 3
Views: 16185
Reputation: 1084
You need pass -y in apt-get, like so: spawn sudo apt-get install -y openssh-client
-or- expect the install prompt and answer "Y"
Also, you do not need to run two shells, just #!/usr/bin/expect
should suffice.
Upvotes: 2
Reputation: 16428
After doing spawn
on the openssh-client
installation, then you have to expect
for something so that expect
will wait for that. Else, expect
won't bother about anything and will simply exit. In this case, we can wait for an eof
to confirm that the installation is completed.
#!/usr/bin/expect
spawn sudo mkdir /usr/lib/jvm1
# This will match the ": " at the end. That is why $ symbol used here.
expect ": $"; # It will be more robust than giving as below
#expect "\[sudo\] password for hduser:"
send "password\r"
#This will match the literal '$' symbol, to match the '$' in terminal prompt
expect "\\$"
spawn sudo apt-get install openssh-client
expect ": $"
send "password\r"
expect eof { puts "OpenSSH Installation completed successfully. :)" }
In the above code, the reason for using the literal '$' is the reason for the terminal prompt.The manual interaction of the actions as below.
dinesh@VirtualBox:~$ sudo mkdir /usr/lib/dinesh7
[sudo] password for dinesh:
dinesh@VirtualBox:~$
The last line having the $
symbol in the terminal. Based on your terminal, you can customize the prompt. You might be wondering why we have to send the password for the second time. Remember, it is not usual terminal, where giving password for admin suffice to other admin operations till the terminal closed. With expect
, we are spawning the sudo
each time which is different and that is the reason for giving it one more time.
As Andrew pointed out, you don't need to add #!
with bash
path.
Update :
In expect
, the default timeout is 10 seconds
. You can change it by using set
as shown below.
set timeout 60; # Timeout will happen after 60 seconds.
Upvotes: 3