views:

274

answers:

3

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.

+1  A: 

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).

Tryskele
Users with different passwords use this script. thus its not suitable to hardcode the password.
Abhijeet
I did not explain myself properly. What I meant is to give the user to input the password via a command line argument. Not to hardcode the password inside the application.
Tryskele
A: 

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
rahul
+1  A: 

I see two problems:

  1. 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.
  2. In the expect script, change

    set PASSWORD [lindex $argv 3];

to

set PASSWORD [lindex $argv 2]; 

you are skipping an argument.

Colin Macleod