Shell scripts automatically enter passwords via expect script (using expect)

Source: Internet
Author: User

background: In the remote file download, you need to enter the other party's server password, the shell does not support interactive input content, can be implemented in the following two ways I. Embedding expect in shell scripts for password entryexpect is a tool for automatic interactive functionality. Expect is a subprocess that executes a shell script through spawn, monitors the return of the script, and evaluates the interactive input (send) by expect 1. Installing expect installation of Tcl:apt-get install TCL is required firstapt-get Install expect  2.expect Use2.1 A simple input password operation#! /usr/bin/expectSet TimeoutSet Password "123456"spawn sudo rm-rf zzlogicexpect "root123456"send "$password \ n"InteractDescription:First line #! /usr/bin/expect represents the shell interaction mode using expectset is an assignment to a variable passwordSet Timeout 100: Sets the time-out to 100 seconds, and if the shell command to execute is long you can set a longer time-out. Expect exceeded timeout time does not monitor the string to be searched, then does not execute, default timeout is 10 secondsspawn executing the shell script under expectexpect the return of the shell script executed by spawn, whether the field in "" is includedSend: If expect detects the included string, it will enter the contents of the Send, \ n equals the carriage returnInteract: Exit expect return terminal, you can continue to enter, otherwise it will always be expect cannot exit to the terminal command line arguments for 2.2expect[lindex $argv n] gets the index n parameter (index is calculated starting from 0)$ARGC is the number of command-line arguments[lrange $argv 0 0] represents the first parameter[lrange $argv 0 3] represents 1th to 3rd parameters For example, the scp_service.sh file can be executed by the./SCP_SERVICE.SH-RM, when a parameter is assignedSET option [lindex $argv 0] (gets the first parameter stored in the variable option, the argument is index is calculated starting from 0) 2.3if...elif...else ...expect supports if statements,If {condition 1} {condition 1 Execution Statement} elif {condition 2} {Condition 2 Execution Statement} else {Other cases Execute statements}Description:1.if condition with {} to include the condition2.if and subsequent {} must be separated by a space3. Two curly braces must be separated by a space, such as if {} {}, or an error will be Expect:extra characters after Close-brace3. Use {to join the next line, so the if condition needs to be added with the left curly brace {4.else can't put a line on its own, so else go after}2.4 Expect {}, multiline expected, match to which bar to execute whichbackground: Sometimes the expected result after executing the shell is not fixed, it is possible to ask is yes/no, it is possible to enter the password, so you can use expect{}put a multiline statement inside the curly braces, matching from top to bottom, matching to which expect executes which sentence.   call expect in 3.shell to implement loginI do this by executing the expect script in the shell script. Of course, you can pass some of the variables defined in the shell to the expect script as parameter input. Can see an example of my invocation description: After this try some expect, give me the feeling is expect to the format of the requirements relatively high, such as curly braces must have a space ah and so on, so if there are errors, we can carefully observe whether the grammatical format is wrong.  two. Configure the SSH trust on the remote serverthis I do not try, there are many online tutorials, feel long-term words should be more convenient than writing expect, but I think the script is best not always go to the operation of other places, so here I use expect myself to write (of course, also want to practice writing expect)

Shell scripts automatically enter passwords via expect script (using expect)

Related Article

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.