Expect of automated Operational scripting language Practice Learning (1)

Source: Internet
Author: User
Tags sha1

I. Introduction of Expect

Expect is a simple TCL-based scripting language tool, a software suite that enables automatic interaction, with the function of automating human-computer interaction, and the ability to "session" with an interactive program in a way that is set in the script content. Depending on the content of the script, expect can tell what the program will prompt or feedback and what is the correct answer; it is an explanatory scripting language that provides "branching and nesting structures" to guide the process.

While the shell is powerful, it is not possible to implement interactive multi-machine operations such as SSH and FTP, and expect can help us achieve this.

Key Features: Automatic login to remote machines and automatic execution of command and shell scripts, combined with timed tasks for complete automation!
Note: The use of key authentication without a password can also implement this function, but no key can only use the remote machine account and password!

Second, the installation of expect

Installation method can be used in the source code and RPM package installation, such as RPM package installation as an example to explain!
[Email protected] src]# cat/etc/issue
Red Hat Enterprise Linux Server Release 6.2 (Santiago)
Kernel \ r on an \m
[Email protected] src]# uname-a
Linux expect-lab 2.6.32-220.el6.i686 #1 SMP Wed Nov 9 08:02:18 EST. i686 i686 i386 Gnu/linux
[[email protected] src] #ll
Total 260

-rw-r--r--1 root root 262584 Mar 7 expect-5.44.1.15-5.el6_4.i686.rpm
[Email protected] src]# RPM-IVH expect-5.44.1.15-5.el6_4.i686.rpm
Warning:expect-5.44.1.15-5.el6_4.i686.rpm:header V3 rsa/sha1 Signature, key ID C105b9de:nokey
Error:failed dependencies:libtcl8.5.so is needed by expect-5.44.1.15-5.el6_4.i686

Problem: Description missing TCL library file, need to install TCL software!

[email protected] src]# LL
Total 2228
-rw-r--r--1 root root 262584 Mar 7 expect-5.44.1.15-5.el6_4.i686.rpm
-rw-r--r--1 root root 2012712 Mar 7 tcl-8.5.7-6.el6.i686.rpm

[Email protected] src]# RPM-IVH tcl-8.5.7-6.el6.i686.rpm
Warning:tcl-8.5.7-6.el6.i686.rpm:header V3 rsa/sha256 Signature, key ID C105b9de:nokey
Preparing ... ########################################### [100%]
1:TCL ########################################### [100%]
[Email protected] src]# RPM-IVH expect-5.44.1.15-5.el6_4.i686.rpm
Warning:expect-5.44.1.15-5.el6_4.i686.rpm:header V3 rsa/sha1 Signature, key ID C105b9de:nokey
Preparing ... ########################################### [100%]
1:expect ########################################### [100%]
[email protected] src]# expect
Expect1.1> exit
#说明expect已经安装成功!

III. instructions for use of expect

expect's core command is spawn expect send set
Note: Expect internal commands can only be executed after the expect is installed, after entering the expect environment!
spawn #调用要执行的命令或脚本
Expect #等待命令提示信息的出现, that is, the prompt to capture user input
Send #发送需要交互的值, instead of user input manually
set #设置变量值
Interact #执行完成后保持交互状态把控制权交给控制台, this time can be manually operated; If you do not have this sentence will exit after logging in, rather than stay on the remote terminal!
Expect EOF #这个一定要加, which corresponds to the spawn to indicate that the capture terminal output information terminates, similar to if....endif
Note: The expect script must end with interact or expect EOF, and performing an automated task typically means that the Script task is executed with expect EOF!
Common syntax settings:
set timeout-1 #设置expect永不超时
Set timeout #设置expect 300-second timeout, If more than 300 no expect content appears, exit the
exp_continue #继续执行接下来的交互操作
$argv parameter array #使用 [lindex $argv N] to get the arguments passed from bash (where n starts from 0 ..., 1th, 2nd, respectively. Parameters)
cmd arg1 arg2 arg3 #说明: The command is delimited by a space, CMD is the command name, and the rest is the command parameter------+/--* Four, simple instance application 1:

implement non-interactive SSH logins via expect, log in and redirect Ifconfig-acquired information to the file
[[ Email protected] src]# ll
Total 2232
-rw-r--r--1  root root  262584 mar  7  .44.1.15-5.el6_4.i686.rpm
-rwxr-xr-x 1  root root     Apr 05:06 ssh_exp
- rw-r--r--1  root root 2012712 mar  7  tcl-8.5.7-6.el6.i686.rpm
Description: There is no ip_information file before executing the expect script in the current directory!

[email protected] src]# cat ssh_exp#!/usr/bin/ expectset host  "192.168.4.176"   #设定远程主机ipset  passwd  "Passwdstr"  # Set password for SSH login to remote host spawn ssh [email protected] $host   #执行ssh登录expect   #expect匹配用户输入信息 {"yes/no " { send " yes\r "; exp_continue }  #匹配到" "content, send Yes to wrap and continue" password: "{ send   "$passwd \ r"; exp_continue }  #匹配到 "", send password wrap and Continue "#"  { send  "ifconfig  \> /usr/local/src/ip_information\r " }  #匹配到" "content, send ifconfig and redirect files}expect eof # End expect 
[Email protected] src]#/ssh_exp spawn ssh [email protected] [email protected] ' s password:last login:thu APR 18 05 : 07:11 from Networker.semico [[email protected] ~]# ifconfig >/usr/local/src/ip_information part Ssh_ The exp script automatically executes the part!

[email protected] src]# LL

total 2236
-rw-r--r--1 root root  262584 Mar   7  expect-5.44.1.15-5.el6_4.i686.rpm
-rw-r--r--1 root root     958 APR 18 05:18 ip_information
-rwxr-xr-x 1 root root     Apr 05:06 ssh_exp
-rw-r--r--1 root root 2012712 mar  7   tcl-8.5.7-6.el6.i686.rpm
Description: The Ip_information file was automatically generated after executing the expect script in the current directory!

[[email protected] src]# cat ip_information  eth0       Link encap:Ethernet  HWaddr 00:0C:29:04:49:6A              inet addr:192.168.4.176  Bcast:192.168.4.255   Mask:255.255.255.0     inet6 addr: fe80::20c:29ff:fe04:496a/64  Scope:link           up broadcast running  MULTICAST  MTU:1500  Metric:1            RX packets:72659 errors:0 dropped:0 overruns:0 frame:0            TX packets:38035 errors:0 dropped:0  overruns:0 carrier:0  collisions:0 txqueuelen:1000            &nbsP rx bytes:18140682  (17.3 MIB)   TX bytes:13117764  (12.5 MIB)             interrupt:18 base address:0x2000lo link  encap:LocalLoopback   inet addr:127.0.0.1  Mask:255.0.0.0            inet6 addr: ::1/128 Scope:Host            up loopback running  mtu:16436   metric:1           rx packets:119051  errors:0 dropped:0 overruns:0 frame:0            TX packets:119051 errors:0 dropped:0 overruns:0 carrier:0   collisions:0 txqueuelen:0            rx  bytes:55243096  (52.6 MIB)   TX bytes:55243096  (52.6 MIB) 

Note: This example implements SSH automatic non-interactive login with a simpler expect script and redirects the obtained interface information to the file after the login is successful ip_information!

This article is from the "Bruce_tan" blog, make sure to keep this source http://380281.blog.51cto.com/370281/1904101

Expect of automated Operational scripting language Practice Learning (1)

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.