The expect script synchronizes files, specifies the host and files to be synchronized, builds a file distribution system, and executes commands remotely in batches

expect script to automatically synchronize files

#!/usr/bin/expect
set passwd "1q2w3e"
spawn rsync -av [email protected]:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
The expect script synchronizes files, specifies the host and files to be synchronized, builds a file distribution system, and executes commands remotely in batches

If you try to cancel the last line, expect eof
The expect script synchronizes files, specifies the host and files to be synchronized, builds a file distribution system, and executes commands remotely in batches
will appear, and if you exit without entering the password
The expect script synchronizes files, specifies the host and files to be synchronized, builds a file distribution system, and executes commands remotely in batches
, there is no file in the /tmp directory.
The expect script synchronizes files, specifies the host and files to be synchronized, builds a file distribution system, and executes commands remotely in batches

Specify the host and the file to be synchronized, from this machine to the other party
#!/usr/bin/expect
set passwd "1q2w3e"
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
The expect script synchronizes files, specifies the host and files to be synchronized, builds a file distribution system, and executes commands remotely in batches

Implementation of a file distribution system

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

ip.list content
[root@aminglinux01 sbin]# cat /tmp/ip.list
192.168.67.129
192.168.67.130
127.0.0.1

file.txt内容
[root@aminglinux01 sbin]# cat /tmp/file.txt
/tmp/12.txt
/root/shell/1.sh
/root/111/222/lll.txt

rsync.sh content
[root@aminglinux01 sbin]# cat rsync.sh
#!/bin/bash
for ip in 'cat /tmp/ip.list'
do
./rsync.expect $ip /tmp/file.txt doneThere is
a
problem , not yet resolved
The expect script synchronizes files, specifies the host and files to be synchronized, builds a file distribution system, and executes commands remotely in batches

If you find the problem, you can modify the punctuation. Forgot to 的作用了<br/>#!/bin/bash<br/>for ip incat /tmp/ip.list` * Note: It's these two punctuation marks that are not single quotes
do
./rsync.expect $ip /tmp/file.txt
done

Experimental result:
[root@aminglinux01 sbin]# sh -x rsync.sh
++ cat /tmp/ip.list

  • for ip in 'cat /tmp/ip.list'
  • ./rsync.expect 192.168.67.129 /tmp/file.txt
    spawn rsync -avR --files-from=/tmp/file.txt / [email protected]:/
    The authenticity of host '192.168.67.129 (192.168.67.129)' can't be established.
    ECDSA key fingerprint is d3:a8:9e:20:25:db:7c:43:d3:a7:48:5c:13:da:34:6b.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '192.168.67.129' (ECDSA) to the list of known hosts.
    [email protected]'s password: 1q2w3e
  • for ip in 'cat /tmp/ip.list'
  • ./rsync.expect 192.168.67.130 /tmp/file.txt
    spawn rsync -avR --files-from=/tmp/file.txt / [email protected]:/
    [email protected]'s password:
    building file list ... done
    root/
    root/111/
    root/111/222/
    root/111/222/lll.txt
    root/shell/
    root/shell/1.sh
    tmp/
    tmp/12.txt

sent 473 bytes received 84 bytes 1114.00 bytes/sec
total size is 174 speedup is 0.31

  • for ip in 'cat /tmp/ip.list'
  • ./rsync.expect 127.0.0.1 /tmp/file.txt
    spawn rsync -avR --files-from=/tmp/file.txt / [email protected]:/
    The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
    ECDSA key fingerprint is d3:a8:9e:20:25:db:7c:43:d3:a7:48:5c:13:da:34:6b.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.

After verification, it was found that the file was synchronized to 67.130, and the
synchronization was successful
[root@aminglinux01 sbin]# sh -x rsync.sh
++ cat /tmp/ip.list

  • for ip in 'cat /tmp/ip.list'
  • ./rsync.expect 192.168.67.129 /tmp/file.txt
    spawn rsync -avR --files-from=/tmp/file.txt / [email protected]:/
    [email protected]'s password:
    building file list ... done
    root/
    root/111/
    root/111/222/
    root/111/222/lll.txt
    root/shell/
    root/shell/1.sh
    tmp/

sent 345 bytes received 65 bytes 820.00 bytes/sec
total size is 174 speedup is 0.42

  • for ip in 'cat /tmp/ip.list'
  • ./rsync.expect 192.168.67.130 /tmp/file.txt
    spawn rsync -avR --files-from=/tmp/file.txt / [email protected]:/
    [email protected]'s password:
    building file list ... done
    tmp/

sent 162 bytes received 15 bytes 354.00 bytes/sec
total size is 174 speedup is 0.98

  • for ip in 'cat /tmp/ip.list'
  • ./rsync.expect 127.0.0.1 /tmp/file.txt
    spawn rsync -avR --files-from=/tmp/file.txt / [email protected]:/
    [email protected]'s password:
    building file list ... done

sent 159 bytes received 12 bytes 114.00 bytes/sec
total size is 174 speedup is 1.02
can go to 67.129 67.130 to verify whether the file has been synchronized, the figure is omitted here.

exe.expect execute command

#!/usr/bin/expect
set host [lindex $argv 0]
set passwd “1q2w3e"
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"

#!/bin/bash
#!/bin/bash
for ip in cat /tmp/ip.list
do
./exe.expect $ip "hostname"
done
The expect script synchronizes files, specifies the host and files to be synchronized, builds a file distribution system, and executes commands remotely in batches

Guess you like

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