简体   繁体   中英

How to pass Username and password to an application using shell script?

I want to make script to login into application, its flow is like

  1. Go to perticular directory,(My script=> cd /ld62_prod)
  2. Give one command which will launch application(My script=> drv)
  3. Application will launch and cursor will be pointing where i need to give username.
  4. After giving username it will display next page prompting for password(now cursor is here).

Now my problem is I am able to reach till application launching, but not able to provide Username and password through script.

My sample script is

cd cd /ld62_prod
drv
112233 #username
112233 #password

What I am doing wrong.

Without Security :

You can use this :

 echo "User name: $0"
 echo "Password: $1"

And you can use the particular values using $0 and $1 .

Refer Shell script for more details .

With Security :

read -s -p "Password: " password

Under Linux (and cygwin) this form works in bash and sh. It may not be standard Unix sh, though.

For more info and options, in bash, type "help read".

$ help read
read: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
Read a line from the standard input and split it into fields.
  ...
  -p prompt output the string PROMPT without a trailing newline before
            attempting to read
  ...
  -s                do not echo input coming from a terminal

You could use expect , which is a standard tool that can communicate with interactive programs/scripts like yours. Its man page says:

Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be. An interpreted language provides branching and high-level control structures to direct the dialogue. In addition, the user can take control and interact directly when desired, afterward returning control to the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM