expect 脚本同步文件,指定host和要同步的文件,构建文件分发系统,批量远程执行命令

expect脚本自动同步文件

#!/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

如果尝试取消最后一行,expect eof

会出现,还没输入密码就退出的情况

/tmp目录下也没文件

指定host和要同步的文件,从本机到对方
#!/usr/bin/expect
set passwd "1q2w3e"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av $file [email protected]$host:$file
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

文件分发系统的实现

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

ip.list内容
[[email protected] sbin]# cat /tmp/ip.list
192.168.67.129
192.168.67.130
127.0.0.1

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

rsync.sh 内容
[[email protected] sbin]# cat rsync.sh
#!/bin/bash
for ip in ‘cat /tmp/ip.list‘
do
./rsync.expect $ip /tmp/file.txt
done
出现问题,暂未解决

发现问题点,修改标点符号,即可。忘了的作用了<br/>#!/bin/bash<br/>for ip incat /tmp/ip.list` * 注意:就是这两个不是单引号的标点符号
do
./rsync.expect $ip /tmp/file.txt
done

实验结果:
[[email protected] 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.

验证之后发现 文件同步到67.130上了
再同步一次就成功了
[[email protected] 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
可以到67.129 67.130 验证文件是否同步过去,此处图省略。

exe.expect 执行命令

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

原文地址:http://blog.51cto.com/13528516/2108370

时间: 2024-08-01 22:39:12

expect 脚本同步文件,指定host和要同步的文件,构建文件分发系统,批量远程执行命令的相关文章

expect脚本同步文件expect脚本指定host和要同步的文件 构建文件分发系统批量远程执行命令

20.31 expect脚本同步文件#!/usr/bin/expectset passwd "liang.123"spawn rsync -av [email protected]:/tmp/12.txt /tmp/ 将远程的/tmp/12.txt同步到本地的机器上 expect {"yes/no" { send "yes\r"} 第一次会提示yes或no"password:" { send "$passwd\r&q

expect脚本同步文件 expect脚本指定host和要同步的文件 构建文件分发系统 批量远程执行命令

自动同步文件 #!/usr/bin/expect set passwd "123456" spawn rsync -av [email protected]192.168.133.132:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof 指定host和要同步的文件

expect脚本同步文件,expect脚本指定host和要同步的文件,构建文件分发系统,批量远程执行

expect脚本同步文件 在一台机器上把文件同步到多台机器上 自动同步文件 [[email protected] sbin]# vim 4.expect #!/usr/bin/expect set passwd "s5381561" spawn rsync -av [email protected]:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:"

expect脚本同步文件、指定host和要同步的文件、构建文件分发系统、批量远程执行命令

expect脚本同步文件 1.自动同步文件 [[email protected] shell]# vi 4.expect 增加如下脚本内容: #!/usr/bin/expect set passwd "123456" spawn rsync -av [email protected]:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send &

expect脚本同步文件、expect指定host和要同步的文件、构建文件分发系统、批量远程执行命令

  一.expect脚本同步文件 自动同步文件 ,把远程的文件同步到本机 cd /usr/local/sbin 1.脚本内容: #!/usr/bin/expectset passwd "123456"spawn rsync -av [email protected]:/tmp/12.txt /tmp/ expect {"yes/no" { send "yes\r" }"password:" { send "$pass

expect脚本同步文件、指定host和要同步的文件、 构建文件分发系统及批量远程执行命令

一. expect脚本同步文件 1.vi 1.expect 内容如下: #!/usr/bin/expect set passwd "123456" spawn rsync -av [email protected]:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof

20.31 expect脚本同步文件;20.32 expect脚本指定host和要同步的文件;20.33 构建文件分发系统;20.34

20.31 expect脚本同步文件 自动同步文件 1. 同步远程机器hao2上/tmp/12.txt文件 到本机/tmp/下: [[email protected] ~]# vim 4.expect 添加内容: #!/usr/bin/expect set passwd "admin" spawn rsync -av [email protected]192.168.211.129:/tmp/12.txt /tmp/ expect { "yes/no" { send

expect脚本同步文件、expect脚本指定host和要同步的文件、构建文件分发系统、批量远程执行

20.31 expect脚本同步文件 自动同步文件 #!/usr/bin/expect set passwd "rootroot" spawn rsync -av [email protected]:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r"} } expect eof 执行 [[ema

20.31 expect脚本同步文件 20.32 expect脚本指定host和要同步的文件 20.

20.31 expect脚本同步文件 20.32 expect脚本指定host和要同步的文件 20.33 构建文件分发系统 20.34 批量远程执行命令 原文地址:http://blog.51cto.com/12058686/2108318