shell-writing a distribution system

Source: Internet
Author: User
Tags rsync

Distribution system: Used for multiple server code or software simultaneous distribution of execution

1.install–y Expect installation expect program for automatic login to remote server

2. Write Automatic Login Script

Vim Hu.expect

#! /usr/bin/expect

Set Host "192.168.***.***"

# Define host is "192.168.***.***" This is the unique way to define variables expect

Set passwd "123456"

# The definition password is 123456

Spawn ssh [email protected]

# perform telnet operation, ssh username is root remote host is 192.168.***.***

Expect {

"yes/no"{send "yes/r"; Exp_continue}

"Assword:" {send "passwd\r"}

}

# Here is the action of interacting with the shell at remote time , when the first remote host, will ask whether remote this host "yes/no" Answer Yes when the password is entered to enter the password is 123456

Interact

# end;expect unique end field

3. After writing the login, enter the command and launch the script

Vim Hu2.expect

Set user "root"

Set passwd "123456"

Set Host "192.168.***.***"

Spawn [email protected] $host

Expect {

"Yes/no" {send "yes/r"; Exp_continue}

"Password:" {send "$passwd \ r"}

}

Expect "]*" # "*" stands for command Prompt "$|#"

Send "touch/tmp/12.txt\r"

Expect "]*"

Send "echo 1212 >/tmp/12.txt\r"

Expect "]*"

Send "exit\r"

4. Write scripts that can pass parameters

Vim Hu3.expect

Set User[lindex $argv 0] # defines how user is to be passed, and is the first parameter

Set Host[lindex $argv 1] # defines how the host is to be passed as a parameter, and is the second argument

Set passwd "123456"

Set cm [lindex $argv 2] # defines how the command is to be passed as a parameter, and is the third argument

Spawn ssh [email protected] $host

Expect {

"Yes/no" {send "yes\r"}

"Password" {send "passwd\r"}

}

Expect "]*"

Send "$cm \ r" # each time a send action is executed, the last parameter is written with a \ R, which is also unique to expect .

Expect "]*"

Send "exit\r"

5.write a script that specifies the synchronization directory specified by the host

Vim Hu4.expect

#! /usr/bin/expect

Set passwd "123456"

Set host [lindex $ARGV 0] # to specify hosts by way of a parameter

Set file [lindex $argv 1] # Specify the synchronization directory by way of a parameter

Spawn Rsync-av $file [email protected] $host: $file # Perform login host sync directory action

Expect {

"Yes/no" {send "yes\r"}

"Password:" {send "passwd\r"}

}

Expect EOF

began the formal preparation of the distribution system

1.requires both native and remote machines to install rsync and the following rsync.expect and rsync.sh scripts must be in the same directory

2. Writing rsync.expect scripts

main command: rsync-av--files-from= $file/[email protected] $host:/

All remote host passwords must be consistent

Vim Rsync.expect

#!/usr/bin/expect

Set passwd "123456"

Set host [lindex $ARGV 0]

Set file [lindex $argv 1]

Spawn Rsync-av--files-from= $file/[email protected] $host:/

Expect {

"Yes/no" {send "yes\r"}

"Password:" {send "$passwd \ r"}

}

Expect EOF

3.vim ip.txt make a list of all the remote hosts and write them in .

4,vim file.list all the files needed to synchronize the full path into a list written into; must be written in full path, starting from / root

5.Write rsync.sh script for rsync.expect script to read the list of remote host IP and all files that need to be synchronized

Vim rsync.sh

#!/bin/bash

For IP in ' cat ip.txt '

Do

Echo $ip

./rsync.expect $ip List.txt

Done

Note: The file synchronization is complete, this time need to log on to the host to perform the restart Service command, so you need the following script help

6. Write commands to execute the script in bulk

Vim Exe.expect

#! /usr/bin/expect

Set User[lindex $argv 0] # defines how user is to be passed, and is the first parameter

Set Host[lindex $argv 1] # defines how the host is to be passed as a parameter, and is the second argument

Set passwd [lindex $argv 2]

Set cm [lindex $argv 3] # defines how the command is to be passed as a parameter, and is the third argument

Spawn ssh [email protected] $host

Expect {

"Yes/no" {send "yes\r"}

"Password" {send "$passwd \ r"}

}

Expect "]*"

Send "$cm \ r" # each time a send action is executed, the last parameter is written with a \ R, which is also unique to expect .

Expect "]*"

Send "exit\r"

7, also need to write a loop remote host IP and execute the command script

Vim exe.sh

#! /bin/bash

For IP in ' cat ip.txt '

Do

Echo $ip

./exe.expect $ip "W;free-m;ls/tmp"

# only three commands are executed here, and if there are many commands, you need to write a script and sync it to each remote server, then write a command to execute that script in this script (full path required)

Done

For more technical documentation, please add Linux system Operations Communication Group: 260431835 regular weekly knowledge of Linux systems


This article is from the "Linux system operation and Maintenance Technology" blog, please be sure to keep this source http://fxlinux.blog.51cto.com/4926551/1874209

shell-writing a 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.