Very simple expect script but does not work - expect

I've been trying to automatically decrypt encrypted Ansible yaml files. I know you can just set the ansible.cfg with
vault_password_file = ~/.vault_pass.txt
But well, I was curious. Turns out I'm unable to do a simple task. Here is the dumb script
#!/usr/bin/expect
set timeout 9
set file [lindex $argv 0]
spawn ansible-vault decrypt $file
expect "Vault password:"
send "MyAwesomePassword\r"
Then I use the script as
ansible-vault decrypt vars-mysql-config.yml
Output is
spawn ansible-vault decrypt /Users/ruben/ansible/vars/vars-mysql-config.yml
Vault password: %
No success. I know this is a stupid question but is so simple and I feel so stuck that I thought I forgot something.
Any ideas? thanks for reading!

Try this:
#!/usr/bin/expect
set timeout 9
set file [lindex $argv 0]
spawn ansible-vault decrypt $file
expect "Vault password: "
send "MyAwesomePassword\r"
interact
I have added a space in expect command and added interact command in end.

Related

expect command not working as expected but working if not using expect script

So I have this script to create a user in my openvpn and the create_user.sh is working fine. But I want to create a bulk user for my openvpn and when I run this expect script it's also working fine, its response exactly to the prompt of the openvpn easyrsa. But the problem comes if I test the user and try to connect to openvpn it gives me error.
#!/usr/bin/expect
set user [lindex $argv 0]
set pass1 [lindex $argv 1]
set pass2 [lindex $argv 2]
set phrase [lindex $argv 3]
set timeout -1
spawn ./create_user.sh
expect "Enter Username:" { send -- "$user\r" }
expect "Enter PEM pass phrase:" { send -- "$pass1\r" }
expect "Verifying - Enter PEM pass phrase:" { send -- "$pass2\r"}
expect "Enter pass phrase for /etc/openvpn/pki/private/ca.key:" { send -- "$phrase\r"}
expect eof
This error I received
I know that the problem is in my PEM pass and pass phrase but when I use my create_user.sh its working fine and it can connect to the openvpn server. So I guess my expect script is not working fine when passing the values to the prompt of PEM pass phrase and pass phrase. Can somebody help me on this I'm just newbie in expect scripting.
This is the prompt when the create_user.sh run.

Bash - How do I write a script that will ssh into a given ip?

How would I write a Bash script that takes a single param (the ip of my remote server) and sshs into it? I tried searching for a ready-made solution, but found nothing so I apologize if this is a dupe of another question.
If all you have to do is ssh it's very short:
#!/bin/bash
ssh user#$1
I suppose it would prompt for a password?
Then expect is what you'd want:
#!/usr/bin/expect
set ip [lindex $argv 0]
spawn user#$ip
expect "assword:"
send "<password>\r"
I actually was able to figure out with a little trial and error. I just put this guy in my .bashrc file and it works like a charm.
ipssh () {
ssh root#$1 -i ~/.ssh/$2;
}

Read .properties file to set value in use and password field in Expect shell script

How to read values from Properties file for user and Password field in expect in shell?
Suppose if i have values.prop file having values like below,
user=abcd
pwd=xxxx
I want to set the above value in my test.sh as below
!/usr/bin/expect
set user "abcd"
set password "xxxx"
spawn ssh $user#$host
expect "$user#$hosts's password:"
send "$password\n"
If you can modify the format of the properties file then the easiest way would be to use the source functionality of tcl (expect script are practically tcl scripts).
!/usr/bin/expect
source values.prop
spawn ssh $user#$host
expect "$user#$hosts's password:"
send "$password\n"
With values.prop now looking like this:
set user "abcd"
set password "xxxx"
Here is a crude shell script which can automatically translate the properties file to a tcl file
#!/bin/sh
. "$1"
printf 'set user "%s"\n' "$user"
printf 'set password "%s"\n' "$password"
Use like this:
translate ./values.prop > values.prop.tcl
Then source values.prop.tcl in your expect script.

Unix-Expect:Capture the output of a command as a expect variable

I am trying to capture the output of a program in unix, and use it as a variable in the expect script. When I excecute the program, I will get something like this:
[user#svr]# ./passwdgenerator
1234*&^^[user#svr]#
As you can see, the "1234*&^^" is the password I want to capture and put it into the expect script:
set user user1
set password [open ./passwdgenerator]
spawn ssh $user#server1.com
expect "password:"
send "$password\r"
Obviously it's not working currently, just wonder how I should write this. Have tried this on the txt file before, worked. but when it is a program output, and for security reason we don't want to store password in the text file, so it doesn't work. Any expert could give me a hand? Greatly greatly appreciated.!
This line
set password [open ./passwdgenerator]
change to
set password [exec ./passwdgenerator]

how to use a shell script to supply a password when the interface asks for it

I have a script(dobrt) which upon executing asks for a password.How can i write a script which executes dobrt and automatically supplies the password it asks for.
when i execute ./dobrt -p file.txt , the system asks for a password. I want the password to be sent in automatically by the script. Here is the output
$ ./dobrt -p file.txt
Found 194 tests to execute
------------ 2010 February 11 11:27:33 ------------
Password: ***************
I tried using shell and expecxt scripts for this. here is what i did.
I have 2 scripts. I call the second script(run_dobrt.exp) from the first one(run_dobrt.sh).
Script 1 : run_dobrt.sh
#!/bin/ksh
TESTCASE_HOME="/home/abhijeet/code/testcases";
TESTCASE_LIST="file.txt";
PASSWORD="*****";
echo "Running Expect Script"
`./run_dobrt.exp $TESTCASE_HOME $TESTCASE_LIST $PASSWORD`
Script 2: run_dobrt.exp
#!/usr/local/bin/expect -f
set TESTCASE_HOME [lindex $argv 0];
set TESTCASE_LIST [lindex $argv 1];
set PASSWORD [lindex $argv 3];
set timeout 200
spawn $TESTCASE_HOME/dobrt -p $TESTCASE_HOME/$TESTCASE_LIST
expect "*?assword:*" {send -- "$PASSWORD\r";}
expect eof
Now when i run run_dobrt.sh i get the following error
run_dobrt.sh[20]: spawn: not found
How to get rid of this error and get this task done? Please help.
What is dobrt? is a self-made program? If this is the case I think you will have to recode it to parse an extra argument that accepts the password. Then you will be able to pass this passowrd to dobrt just as you do it like "-p file.txt" in the command line (through a script).
I see two problems:
In the last line of your shell script, remove the back-quotes `` around the command,
they will cause the output of the expect script to be executed as a shell command.
In the expect script, change
set PASSWORD [lindex $argv 3];
to
set PASSWORD [lindex $argv 2];
you are skipping an argument.
If the password is the only input dobrt prompts for, you could try this:
Script 1 : run_dobrt.sh
#!/bin/ksh
TESTCASE_HOME="/home/abhijeet/code/testcases";
TESTCASE_LIST="file.txt";
PASSWORD="*****";
./run_dobrt.exp $TESTCASE_HOME $TESTCASE_LIST << EOF
$PASSWORD
EOF

Resources