Shell "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, this time only know the other machine's account 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

First look at a script that does not exit after login

Vim 1.expect #需使用expect解释 #! /usr/bin/expectset Host "192.168.1.202" set passwd "504360522" spawn ssh-p60522 [email protected] $host #my ssh PO RT is 60522,not is 22expect {"yes/no" {send "yes\r"; exp_continue} "password:" {send "$passwd \ r"}}chmod +x 1.expect./1.e Xpect

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

Vim 2.expect#!/usr/bin/expectset user "root" set passwd "504360522" Spawn ssh-p 60522 [email protected]expect {"yes/no" {s End "Yes\r"; Exp_continue} "Password:" {send "$passwd \ r"}}expect "]*" send "touch/tmp/szk.txt\r" expect "]*" send "echo Szk >/tmp/sz K.txt\r "expect"]* "send" exit\r "chmod +x 2.expect./2.expect


3. We can also pass parameters

vim 3.expect#!/usr/bin/expectset user [lindex  $argv  0]set host [lindex   $ARGV  1]set passwd  "504360522" set cm [lindex  $argv  2]spawn ssh  -p 60522 [email protected] $hostexpect  {"yes/no"  { send  "yes\r"} " Password: " { send " $passwd \ r " }}expect "]* "send " $cm \ r "expect "]* "send   "exit\r" test: [[email protected] shell]# chmod +x 3.expect[[email protected]  shell]#./3.expect root 192.168.1.202 ls     #加上   Users  IP    Execute command parameters spawn ssh -p 60522 [email protected][email protected] ' s  password:last login: sat mar 12 10:40:11 2016 from 192.168.1.201[[ email protected] ~]# ls /rootanaconda-ks.cfg  install.log   Install.log.syslog

4. Automatically synchronize files

Vim 4.expect#!/usr/bin/expectset passwd "504360522" Spawn rsync-avz-e "ssh-p 60522" [Email protected]:/tmp/szk.txt/tmp/ Expect {"yes/no" {send "yes\r"} "Password:" {send "$passwd \ r"}}expect EOF test: [[email protected] shell]# chmod +x 4.expect [Email protected] shell]#/4.expectspawn rsync-avz-e ssh-p 60522 [email protected]:/tmp/szk.txt/tmp/[email protected ] ' s password:receiving incremental file listszk.txtsent bytes received bytes 73.33 bytes/sectotal size is 4 speed Up is 0.04

5. specify hosts and synchronized files

vim 5.expect #!/usr/bin/expectset passwd  "504360522" set host [lindex  $argv  0]set file [lindex  $argv  1]spawn rsync  -ave   "ssh -p 60522"   [email protected] $host: $file   $fileexpect  {"yes/no"  { send  "yes\r"} "Password:"  { send  "$passwd \ r"  }}expect  eof test: [[email protected] shell]# chmod +x 5.expect [[email protected]  shell]# ./5.expect 192.168.1.202 /tmp/szk.txtspawn rsync -ave ssh -p  60522 [email protected]:/tmp/szk.txt /tmp/szk.txt[email protected] ' S password: Receiving incremental file listsent 11 bytes  received 37 bytes   96.00 bytes/sectotal size is 4  speedup is 0.08 

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]:/


4. Implementation of the document distribution system

vim  rsync.expect#!/usr/bin/expectset passwd  "504360522" set host [lindex $ argv 0]set file [lindex  $argv  1]spawn rsync -ave  "ssh -p  60522 " --files-from= $file  / [email protected] $host:/expect {" yes/no " { send   "yes\r"} "Password:"  { send  "$passwd \ r"  }}expect eof================vim    rsync.sh     #写一个循环脚本 #!/bin/bashfor ip in  ' cat ip.list ' do     echo  $ip     ./rsync.expect  $ip  list.txtdone======== ========vim ip.list        #ip列表192 .168.1.202vim list.txt       #文件列表/tmp/szk.txt                           ================ test: [ [EMAIL PROTECTED]&NBsp;shell]# chmod +x rsync.sh rsync.expect[[email protected] shell]# ./ rsync.sh192.168.1.202spawn rsync -ave ssh -p 60522 --files-from=list.txt /  [email protected]:/[email protected] ' s password:building file list   donetmp/sent 54 bytes  received 15 bytes  46.00 bytes/ sectotal size is 4  speedup is 0.06


5. command batch execution script

vim exe.expect#!/usr/bin/expectset host [lindex  $argv  0]set passwd  " 504360522 "set cm [lindex  $argv  1]spawn ssh -p 60522 [email  Protected] $hostexpect  {"yes/no"  { send  "yes\r"} "Password:"  { send  "$passwd \ R " }}expect "]* "send " $cm \ r "expect "]* "send " exit\r "================#!/bin/bashfor  ip in  ' cat ip.list ' do    echo  $ip     ./ exe.expect  $ip   "w;free -m;ls /tmp" Done test: [[[Email protected] shell]# ./ Exe.sh192.168.1.202spawn ssh -p 60522 [email protected][email protected] ' s  password:last login: sat mar 12 15:49:01 2016 from 192.168.1.201[[ email protected] ~]# w;free -m;ls /tmp 15:50:28 up  5:56,   3 users,  load average: 0.01, 0.01, 0.00user     tty       FROM              [email  protected]   idle   jcpu   pcpu whatroot      tty1     -                 09:54    5:55m  0.04s  0.04s  -bashroot     pts/0    192.168.1.112    09:54    33:20   0.19s  0.19s -bashroot      pts/1    192.168.1.201    15:50    0.00s   0.15s  0.08s w              total   &nbSp;   used       free     shared     buffers     cachedMem:            980        359         621          0          10        252-/+ buffers/ cache:         96         884swap:         1983           0       1983szk.txt  yum.log  yum_ Save_tx-2015-12-28-16-01qedbzm.yumtx


This article is from the "Fuqin Wine" blog, please make sure to keep this source http://szk5043.blog.51cto.com/8456440/1751316

Shell "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.