Expect spawn, Linux expect usage small memory __linux

Source: Internet
Author: User
Use expect to implement automatic login script, there are many online, but there is no clear explanation, beginners are generally copied, collection. But why to write this but do not know it. This article uses a shortest example to illustrate the principle of the script.
The script code is as follows:
##############################################
#!/usr/bin/expect
Set Timeout 30
Spawn ssh-l username 192.168.1.1
Expect "Password:"
Send "ispass\r"
Interact
##############################################
1. [#!/usr/bin/expect]
This line tells the operating system that the code in the script uses that Shell to execute. Here's the expect in fact and Linux under Bash, Windows under the CMD is a kind of thing.
Note: This line needs to be in the first line of the script.
2. [Set timeout 30]
Basically know English are aware that this is set timeout time, now you just remember his unit of timing is: seconds
3. [Spawn ssh-l username 192.168.1.1]
Spawn is the expect internal command that can be executed after entering the expect environment, and cannot find the spawn command if it is not installed expect or executed directly under the default shell. So do not use the "which spawn" order to find the Spawn command. Like the dir in Windows is an internal command, which is brought by the shell and you cannot find a dir.com or Dir.exe executable file.
Its main function is to add a shell to the SSH running process to pass the interactive instruction.
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, the habit is good. The meaning of this command is to determine whether the last output contains "password:" string, if there is a return immediately, or wait for a period of time to return, where the waiting length is the previous set of 30 seconds
5. [Send "ispass\r"]
This is the interactive action, which is equivalent to the manual input of the password.
Hint: the end of the command string do not forget to add "\ r", if there is an abnormal waiting state can be checked.
6. [Interact]
After the completion of the implementation of the interactive State, the control authority to the console, this time can be manually operated. If you do not have this login completed, you will quit, rather than stay on the remote terminal. If you just log in past execution
#!/usr/bin/expect #注意安装的路径, not sure Whereis expect.
# Change a login shell to bash
Set user [lindex $argv 0]
Spawn Bash $user
Expect "]:"
Send "/bin/bash"
Expect EOF

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.