Expect spawn, Linux expect usage notes

Source: Internet
Author: User

Use expect to implement automatic logon script, there are many online, but there is no clear explanation, beginners are generally copied, collection. But why do you want to write this but do not know it. This article uses a shortest example to illustrate the principle of scripting.
The script code is as follows:

    1. ##############################################
    2. #!/usr/bin/expect
    3. Set Timeout 30
    4. Spawn ssh-l username 192.168.1.1
    5. Expect "Password:"
    6. Send "ispass\r"
    7. Interact
    8. ##############################################

1. [#!/usr/bin/expect]
This line tells the code in the operating system script to use that Shell to execute. The expect here is actually a kind of thing with bash under Linux and cmd under Windows.
Note: This line needs to be on the first line of the script.
2. [Set timeout]
basically know the English language knows this is set timeout time, now you just remember his timing unit is: Seconds
3. [Spawn ssh-l username 192.168.1.1]
Spaw n is the expect internal command that can be executed after entering the expect environment, if no expect is installed or the Spawn command is not found directly under the default shell. So don't use commands like "which spawn" to find spawn commands. Like dir in Windows is an internal command that comes with the shell and you can't find an executable file for dir.com or Dir.exe. The main function of the
is to add a shell to the SSH running process to pass the interactive instructions.
4. [Expect "password:"]
Here expect is also an internal command of expect, a bit dizzy, expect shell command and internal command is the same, but not a function, habit is good. This command means to determine whether the last output contains the string "Password:", if any, return immediately, or wait for a period of time to return, where the waiting time is the previous set of 30 seconds
5. [Send "ISPASSR]
Here is the interactive action, which is equivalent to the action of entering the password manually.
Tip: Do not forget to add "R" to the end of the command string, if there is an abnormal waiting state can be checked.
6. [Interact]
Keep the interaction state after execution, and give control to the console, which can be done manually. If this is not done, it will exit instead of remaining on the remote terminal. If you just log in past execute

    1. #!/usr/bin/expect #注意安装的路径, not sure Whereis expect a bit.
    2. # Change a login shell to bash
    3. Set user [lindex $argv 0]
    4. Spawn Bash $user
    5. Expect "]:"
    6. Send "/bin/bash"
    7. Expect EOF
    8. Exit

Expect spawn, Linux expect usage notes

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.