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

折月煮酒 提交于 2019-12-20 04:07:07

问题


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:


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




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/2242643/how-to-use-a-shell-script-to-supply-a-password-when-the-interface-asks-for-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!