简体   繁体   English

如何使用Shell脚本将用户名和密码传递给应用程序?

[英]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) 转到垂直目录,(我的脚本=> cd / ld62_prod)
  2. Give one command which will launch application(My script=> drv) 给出一个将启动应用程序的命令(我的脚本=> 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 . 您可以使用$0$1来使用特定值。

Refer Shell script for more details . 有关更多详细信息,请参考Shell脚本

With Security : 具有安全性:

read -s -p "Password: " password

Under Linux (and cygwin) this form works in bash and sh. 在Linux(和cygwin)下,这种形式可以在bash和sh中使用。 It may not be standard Unix sh, though. 但是,它可能不是标准的Unix sh。

For more info and options, in bash, type "help read". 有关更多信息和选项,请在bash中键入“帮助阅读”。

$ 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. 您可以使用expect ,这是一个标准工具,可以与像您这样的交互式程序/脚本进行通信。 Its man page says: 它的手册页说:

Expect is a program that "talks" to other interactive programs according to a script. Expect是根据脚本与其他交互式程序“对话”的程序。 Following the script, Expect knows what can be expected from a program and what the correct response should be. 在脚本之后,Expect知道程序可以期望什么,正确的响应应该是什么。 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. 此外,用户可以根据需要进行控制并直接进行交互,然后将控制权返回给脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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