开发者

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:

  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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜