Examples of interactive behaviors such as Automatic password entry for Linux under the background program

Source: Internet
Author: User

To develop a scheduled task today, then join the cron list. But there is a problem in front of the script in the execution of the need to enter the database password:

[Plain]View Plain copy
    1. Mysql-u root-p << SQL
    2. Use DB;
    3. Set names UTF8;
    4. ' Cat Task.sql '
    5. Sql

After running the above code, you will be prompted:

[Plain]View Plain copy
    1. Enter Password:

After searching, we found that expect can help us to do this thing:

Save the above code in a file: db.sh, and write the following code:

[Plain]View Plain copy
    1. #!/usr/bin/expect-f
    2. Spawn db.sh
    3. Expect "Enter Password:"
    4. Send--"password\n"
    5. Interact

Then we will go into the database interface after we run it.

The first line indicates which program the script will use to run;

The second line is the focus, under the expect launched a program we want to implement;

The third line is also the focus, we want to tell expect, when what information appears need expect help us to complete the interaction;

The Forth line is the input password prompt, the content to send;

The meaning of line five is to keep the interaction and don't quit. Without this sentence, the script will exit immediately after it has successfully logged into the database.

http://blog.csdn.net/topasstem8/article/details/17920127

Examples of interactive behaviors such as Automatic password entry for Linux under the background program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.