shell project-distribution system

Distribution System-expect

  yum install -y expect
  //Automatic remote login
 #! /usr/bin/expect
set host "192.168.133.132"
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact

After automatic remote login, execute commands and exit

#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh $user@192.168.133.132

expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*"
send "touch /tmp/12.txt\r"
expect "]*"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"

Transfer parameters

#!/usr/bin/expect

set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "123456"
set cm [lindex $argv 2]
spawn ssh $user@$host

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"

Automatic Synchronization of Files

#!/usr/bin/expect
set passwd "123456"
spawn rsync -av root@192.168.133.132:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

Pass parameters and exit

#!/usr/bin/expect

set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "123456"
set cm [lindex $argv 2]
spawn ssh $user@$host

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"

Specify host s and files to synchronize

#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av $file root@$host:$file
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

Building a file distribution system

 For large companies, there must be updates to websites or configuration files from time to time, and the machines used must be many, few, many dozens or even hundreds. Therefore, it is very important to automatically synchronize files.
 The idea is to have a template machine to prepare the files to be distributed, and then to distribute the synchronized files to the target machine in batches using expect scripts.
 Core command rsync-av -- files-from = list.txt/ root@host:./

# Implementation of Document Distribution System
 rsync.expect content
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
 ip.list content
192.168.133.132
192.168.133.133
......

rsync.sh content

#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./rsync.expect $ip list.txt
done

Command batch execution

#Contents of exe.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "123456"
set cm [lindex $argv 1]
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"

# Contents of exe.sh
#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./exe.expect $ip "w;free -m;ls /tmp"
done

Keywords: Operation & Maintenance rsync ssh yum

Added by svan_rv on Sat, 05 Oct 2019 03:26:09 +0300