Part IV Shell Programming 5 Project Two distribution system

Source: Internet
Author: User
Tags rsync

The first part: expect explanation
Expect allows us to automatically log on to remote machines, and can implement automatic remote execution of commands. Of course, 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, just know the other machine's account number and password can be implemented through the expect script login and remote command.
Before using expect, you need to install expect:
Yum Install-y expect

1. Automatic remote login, and execute the command, login to another machine, the script to interact with the machine, login automatically enter the password
First look at a script that does not exit after login:
1.#! /usr/bin/expect
2.set Host "192.168.11.102"
3.set passwd "123456"
4.spawn ssh [email protected] $host #spawn is a syntax
5.expect {
6. "Yes/no" {send "yes\r"; exp_continue} #第一次登陆系统提示exp_continue往下走
7. "Assword:" {send "$passwd \ r"}
8.}
9.interact


Then look at a script that executes the command and then exits after landing:

1.#!/usr/bin/expect
2.set User "root"
3.set passwd "123456"
4.
5.spawn ssh [email protected]
6.
7.expect {
8. "yes/no" {send "yes\r"; exp_continue}
9. "Password:" {send "$passwd \ r"}
Ten.}
11.expect "]*" #这里定位在输入状态 * means any character root is displayed #, other users display $
12.send "touch/tmp/12.txt\r"
13.expect "]*"
14.send "echo 1212 >/tmp/12.txt\r"
15.expect "]*"
16.send "exit\r"

2. We can also pass parameters
1.#!/usr/bin/expect
2.set User [lindex $argv 0] #第一个是用户
3.set Host [lindex $argv 1] #第二个是IP
4.set passwd "123456"
5.set cm [lindex $argv 2] #第三个cm是一个命令, with a parameter to reflect
6.
7.spawn ssh [email protected] $host
8.
9.expect {
"yes/no" {send "yes\r"}
"Password:" {send "$passwd \ r"}
.}
13.expect "]*"
14.send "$cm \ r"
15.expect "]*"
16.send "exit\r"


Perform chmod +x 3.exp
./3.exp root 192.168.0.16 "ls/tmp/"

===============
Rsync-av 1.expect
A represents a lot of options, including permissions soft connect


The local file 1.expect to the remote machine, if the remote machine directory does not exist, you can automatically create a layer of directories, two layers No
rsync-av/tmp/shell/1.expect [Email protected]:/tmp

Rsync-av/tmp/shell/1.expect 192.168.11.18:/tmp omit the user, login is the current user

Upload files from remote machines to local machines
Rsync-av 192.168.11.18:/tmp/12.txt/tmp/12.txt


This is automatically synchronizing the local machine's files to the remote machine.
3. Automatically synchronize files
1.#!/usr/bin/expect
2.set passwd "123456"
3.spawn rsync-av/tmp/12.txt [Email protected]:/tmp
4.expect {
5. "Yes/no" {send "yes\r"}
6. "Password:" {send "$passwd \ r"}
9.3
8.expect EOF
9.



3. Automatically synchronize files
1.#!/usr/bin/expect
2.set passwd "123456"
3.spawn Rsync-av [email protected]:/tmp/12.txt/tmp/
4.expect {

6." Password: "{send" $passwd \ r "}
7.}
8.expect eof
9.


4. Specify the host and the files to be synchronized, and upload the local files to the remote machine
1.#!/usr/bin/expect
2.set passwd "123456"
3.set host [lindex $ARGV 0]
4.set file [lindex $argv 1]
5.spawn rsync-av $file [email protected] $host: $file
6.expect {
7. "Yes/no" {send "yes\r"}
8. "Password:" {send "$passwd \ r"}
7.3
10.expect EOF


Execution:./4.expect 192.168.11.18/tmp/12.txt
or expect 4.expect 192.168.11.18/tmp/12.txt

Part II: Building a File distribution system
1. Requirements background
For large companies, there must be a site or configuration file updates from time to time, and the use of the machine is certainly a lot of units, less than a few, more than dozens of or even hundreds of sets. Therefore, automatic synchronization of files is critical.

2. Realization of Ideas
Start with a template machine, prepare the files to be distributed, and then distribute the files that need to be synchronized to the target machine in batches using the expect script.
3. Core commands
Rsync-av--files-from=list.txt/[email protected]:/#必须从根目录开始查找, list.txt can write absolute path, can find this file can be, is the path splicing


=====
Perform
Rsync-av--files-from=list.txt/192.168.11.16:/

Create a list of files in a file list.txt first.
/bin/shell/1.sh
/tmp/12.txt

====


4. Implementation of the document distribution system
5.expect
1.#!/usr/bin/expect
2.set passwd "123456"
3.set host [lindex $ARGV 0]
4.set file [lindex $argv 1]
5.spawn rsync-av--files-from= $file/[email protected] $host:/#代表同步的文件列表
6.expect {
7. "Yes/no" {send "yes\r"}
8. "Password:" {send "$passwd \ r"}
7.3
10.expect EOF

#下面可以省略, with the shell instead of
12.cat ip.list
13.192.168.11.18
14.192.168.11.19
15 .....


Implement a machine distribution first
For IP in 192.168.11.16; Do expect 5.expect $ip./list.txt; Done

If you are distributing multiple
For IP in ' cat ip.txt '; Do expect 5.expect $ip./list.txt; Done
=======================

Execution of the script

rsync.sh
1.#!/bin/bash
2.for IP in ' cat ip.list '
3.do
4. Echo $ip
5../5.expect $ip List.txt
6.done
7.


=====================

5. Command batch execution script
Exe.expect
1.#!/usr/bin/expect
2.set host [lindex $argv 0]
3.set passwd "123456"
4.set cm [lindex $argv 1]
5.
6.spawn ssh [email protected] $host
7.
8.expect {
9. "Yes/no" {send "yes\r"}
"Password:" {send "$passwd \ r"}
One .}
12.expect "]*"
13.send "$cm \ r"
14.expect "]*"
15.send "exit\r"

Ip.txt there are 192.168. More than 11.16 can have loops

For IP in ' cat ip.txt '; do./exe.expect $ip "W;LS-DL/TMP; Touch/root/aming123.txt "; Done


==============
Execution of the script

exe.sh
1.#!/bin/bash
2.for IP in ' cat ip.list '
3.do
4. Echo $ip
5../exe.expect $ip ' w;free-m;ls/tmp '
6.done

SSH 192.168.11.16

Part IV Shell Programming 5 Project Two distribution system

Related Article

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.