expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches

20.31 expect script synchronization file
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
#!/usr/bin/expect
set passwd "liang.123"
spawn rsync -av [email protected]:/tmp/12.txt /tmp/ synchronize the remote /tmp/12.txt to On the local machine,
expect {
"yes/no" { send "yes\r"} will prompt yes or no for the first time
"password:" { send "$passwd\r" }
}
expect eof
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
plus execute permission, and then execute the script Check the file
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
to see if there is a synchronized file on the machine.
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
If you comment out the script expect eof
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
and execute the file, it will exit as soon as you log in. If it hasn't arrived yet, transfer the
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
host specified by the 20.32 expect script and the file to be synchronized
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
vi /5.expect this The script is synced by this to the remote
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av $file root@$host:$file first The first parameter specifies the IP The second parameter specifies the file
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
needs to enter the first parameter IP and the second parameter when executing 5.expect the path of the file to be synchronized
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
20.33 Build a file distribution system
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
vi rsync.expect
content
#!/usr/bin/expect
set passwd "liang.123"
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
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
creates list.txt in the /tmp directory, the content of this file is the file path to be synchronized
vi /tmp/list.txt
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
The three files to be synchronized , the premise is that the other machine must also have the same path at the same time. If you are not sure that the other machine also has the same path, you can add an R to the rsync.expect component. When the other machine does not have the same path, the path will be automatically created.
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
Because there may be more than one machine for remote synchronization, all need to create an IP list
vi /tmp/ip.list
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
Before executing the script, make sure that the passwords of the two machines are the same.
Next, continue to create rsync.sh
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
vi /rsync.sh
#!/bin/bash
for ip in cat /tmp/ ip.list
do
echo $ip
./rsync.expect $ip /tmp/file.list
done
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
Before executing, give rsync.expect an execution permission
chmod a+x rsync.expect
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
192.168.137.130 There is no problem with synchronization, but there is a prompt
/root/shell/1.sh" failed: No such file or directory. This 1.sh does not exist.
127.0.0.0 is a problem, and the password is wrong,
but this method of operation There is no problem,
check whether the file
20.34 batch remote execution command is synchronized on the 192.168.137.130 machine
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
cd /usr/local/sbin
vi exe.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd " liang.123"
set cm [lindex $argv 1] cm is the second parameter, cm is the command to be executed
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r"}
}
expect "]"
send "$cm\r"
expect "]
"
send "exit\r"
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
plus execute permission
chmod a+x exe.expect to
define an exe.sh script
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches
vi exe.sh
#!/bin/bash
for ip in cat /tmp/ip.list
do
echo $ip
./exe.expect $ip "hostname"
done
command executed successfully on 2 machines
expect script to synchronize files expect script to specify host and files to be synchronized to build a file distribution system to execute commands remotely in batches

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324980801&siteId=291194637