[TOC]
Distribution System-expect What is a distribution system?
Now some of the larger enterprises, most of the use of load balancing, and sometimes because some programs to change, or some bug to modify, if only a few servers, it is very simple, the already modified procedures to copy the past, or rsync remote push, or online NFS sharing on it can be;
But if there are dozens of hundreds of units, that way will be too cumbersome, I
We can now use expect to batch distribute tasks.
This system, built by expect, can help us update the updated configuration to each server. What is expect?
[] Expect: A software suite for automatic interactivity, a scripting language based on TCL, with simple syntax;
[] Function: Realize automatic login to the remote machine and execute the command automatically; combined with shell script, it can be fully automated;
- [] Note: If you use key authentication without a password, you can also implement automatic logon and automatic remote command execution. But when we can't use key verification, we don't have a choice. So, only then know the other machine's account and password can be implemented through the expect script login and remote command.
Second, remote login 2.1 environmental needs
模板机线上的server
2.2 Template machine configuration
[[email protected] ~]# yum install -y expect
2.3 Automatic telnet, execute command
[[email protected] ~]# cd /usr/local/sbin/[[email protected] sbin]# vim 1.expect#! /usr/bin/expectset host "192.168.XXX.XXX"set passwd "123456"spawn ssh [email protected]$hostexpect {"yes/no" { send "yes\r"; exp_continue} //yes \r是回车的意思"password:" { send "$passwd\r" } //输入密码}interact //表示结束了
When the first connection needs to be confirmed, the direct Yes \ R is the meaning of carriage return, and then continue to enter the password again, this place's password is defined above the other server's password
2.4 Authorizing the connection to run the script
Iii. Script Remote Execution command (after automatic telnet, execute command and exit) 3.1 Modify the script's functionality based on understanding the above script:
vim 1.expect#!/usr/bin/expectset user "root"set passwd "123456"spawn ssh [email protected]expect {"yes/no" { send "yes\r"; exp_continue}"password:" { send "$passwd\r" }}expect "]*" // [root]#或者[host]$send "touch /tmp/12.txt\r"expect "]*"send "echo 1212 > /tmp/12.txt\r"expect "]*"send "exit\r"
3.2 Authorizing the connection to run the script
[[email protected] sbin]# chmod a+x 2.expect[[email protected] sbin]# ./2.expect
A touch/tmp/12.txt file is created and a data is written within the file, and then the remote server exits.
3.3 Check
We log on to the server on the line to see if the file machine content has been created
[[email protected] ~]# cat /tmp/12.txt1212
Iv. Script Pass parameter 4.1 script parameter settings
[[email protected] sbin]# vim 3.expect[[email protected] sbin]# chmod a+x 3.expect#!/usr/bin/expectset user [lindex $argv 0]set host [lindex $argv 1]set passwd "123456"set cm [lindex $argv 2]spawn ssh [email protected]$hostexpect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect "]*"send "$cm\r"expect "]*"send "exit\r"脚本中的 $argv 0 或者 $argv 1就是所谓的执行脚本时候所输入的第一个第二个参数。
Perform tests
The order is: the command executed by the user+ host host+.
V. Automatic synchronization of files 5.1 configuration scripts
(not to be continued)
Shell Project-Distribution system-expect