Use Linux expect command example skillfully

Source: Internet
Author: User


Preface

A period of time before the work of the machine replaced by Ubuntu + 128G SSD + 8G memory desktop, and finally embarked on the use of Linux Office Road, in general, Ubuntu is still more smoothly than I imagined, its "apt-get-style" package management, installation software is very convenient, Apt-get Install XXX can be installed. The only drawback is that the bank's "U shield" and so does not support Linux, so the use of net silver is slightly inconvenient.
In return, the company has done one thing after the change of desktop is to unify the testing environment in the company, all the test environment machine Unified into the computer room with the way of virtual machine Unified Management (happy). In order to develop the test at a later time, the project team has applied for multiple sets of test environments, such as: development environment: The environment for each iterative self-test of the developer. Test environment: A test environment with stable and online code synchronization, typically used to demonstrate or service other applications in the test environment. In addition, there is a set of performance testing environment. The problem with so many environments is that every time you need to update the environment: update code, restarting Tomcat requires SSH to a machine. This is actually a waste of time repetitive labor, usually, this kind of thing can be solved using code. This is our protagonist today: expect.

Expect detailed


The expect command is simply to implement tools that automatically interact in interactive software through its built-in commands. Wiki:http://zh.wikipedia.org/zh/expect
SSH can be used in conjunction with the Spawn command to achieve automatic SSH login. And because it can be written in the script judgment, assignment and other logic, with high flexibility.

Demo examples are as follows:

#!/usr/bin/expect-f # #声明使用哪种解析器解析该脚本, if expect is not installed, use Apt-get install expect installation

if {[Llength $argv] < 1} {# # $ARGV a parameter array to execute the script, determine the length to decide whether to continue
Puts "Usage: $argv 0 need ssh IP"//expect echo,system.out.println ()
Exit 1
}

Set Envs [lindex $argv 0] # #将数组中的第一个参数赋值给 Envs

Set Timeout 30//set timeout of 30 seconds to wait for terminal response
if {$envs = = {# #注意 if and {There is a space between {and $envs, there is a space between the equals sign, there are spaces before and after 61, and there are spaces in the space!!!.}
Set IPs xxx.xx.xxx.61
}

if {$envs = = 141} {
Set IPs xxx.xx.xxx.141
}

if {$envs = = 136} {
Set IPs xxx.xx.xxx.136
}
Spawn ssh root@ $ips # #执行ssh命令实现登陆
Expect {

# #第一次登陆的时候, ask if there is a local save the key, carefully you should find that the match here is a regular, Jiang said, only matching connecting is also possible.
"Are you sure your want to continue connecting (yes/no)?" {send ' yes\r '}
"Password:" {send "cdyanfa\r"}# #保存过一次后, it will return directly to the need for the password phase. Use the Send command to send the password.
}

Interact # #是Expect用来打开用户与产生进程之间通信的命令, to put it simply is to keep the remote server terminal at the current terminal after landing, instead of shutting down the remote terminal.
Just a few lines of command to simplify the operation of the daily repeated n times to a row, is useful for improving efficiency.

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.