Achieve true automation, expect scripting language use

Source: Internet
Author: User

Achieve true automation, expect scripting language use


Several important sentences in the expect:


The core of expect is spawn expect send set


Spawn calling the command to execute


Expect waits for the command prompt to appear, which is the prompt to capture user input:


Send sends values that need to be interacted with instead of manually entering content by the user


Set Variable Value


Interact after the completion of the implementation of the interactive State, the control to the console, this time can be manually operated. If this is not done, it will exit instead of remaining on the remote terminal.


Expect EOF this must be added, corresponding to the spawn to indicate that the capture terminal output information is terminated, similar to IF....ENDIF


Set TIMEOUT-1 indicates no time-out


Set Timeout 10 indicates a time-out of 10 seconds


Note: The expect script must end with interact or expect EOF, and it is often enough to perform an automated task expect EOF.


------------------------------------------------------------

Example: SSH telnet, sending reboot instructions

Vim Reb.expect

#!/usr/bin/expect-f set Timeout ten set username root #定义变量set hostname 192.168.137.28set password redhatspawn ssh "[EMA Il protected] $hostname "#执行命令expect {#捕捉信息的提示, enter" yes/no "{send" yes\r "in order to enter the next command, exp_continue} #exp_continue表示继续下一步" P Assword: "{send" $password \ r "}}expect" # "send" Shutdown-r now\r "#重启命令send" exit\r "#退出登录expect EOF #终止expect的捕捉exit

------------------------------

Implementation stays on the remote host:

#!/usr/bin/expect-f set Timeout 10set username rootset hostname 192.168.137.28set password redhatspawn ssh "[Email Protec Ted] $hostname "expect {" yes/no "{send" yes\r "exp_continue}" password: "{send" $password \ r "}}expect" # "send" LL; Df-ht\r "#send" exit\r "#expect EOF interact #执行完后保持交互的模式, at which point you can manually operate exit

------------------------------------------------------------

Referencing the expect script in the script: Expect-c ""

Vim reb.sh

#!/bin/bash username= "root" hostname= "192.168.137.28" password= "Redhat" expect-c "#-c implement call # #注意凡是-C after the content, if with" "double quotation mark words , must be escaped with \, otherwise the error set timeout 10spawn ssh \ "[email protected] $hostname \" Expect {\ "yes/no\" {send \ "yes\r\"; exp_continue} \ " Password:\ " {Send \ "$password \r\"}} Expect \ "]#\" Send \ "ll\r\" Interactexit "

--------------

Expect-c <<eof

.......

Eof

You can't stay on a remote host when you use this kind of method



This article is from the "Tiandaochouqin" blog, make sure to keep this source http://luzhi1024.blog.51cto.com/8845546/1660276

Achieve true automation, expect scripting language use

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.