标签:rem 思路 HERE emd 定义 ast 命令行 tty 配置文件
expect脚本同步文件[root@akuilinux01 sbin]# vim 4.expect
#!/usr/bin/expect
set passwd "s5381561"
spawn rsync -av root@192.168.21.129:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
[root@akuilinux01 sbin]# chmod a+x 4.expect
[root@akuilinux01 sbin]# ./4.expect
spawn rsync -av root@192.168.21.129:/tmp/12.txt /tmp/
root@192.168.21.129‘s password:
receiving incremental file list
12.txt
sent 43 bytes received 97 bytes 280.00 bytes/sec
total size is 5 speedup is 0.04
[root@akuilinux01 sbin]# cat /tmp/12.txt
1212
[root@akuilinux01 sbin]# vim 5.expect
#!/usr/bin/expect
set passwd "s5381561"
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
[root@akuilinux01 sbin]# chmod a+x 5.expect
[root@akuilinux01 sbin]# ./5.expect 192.168.21.129 /root/akui.txt
spawn rsync -av /root/akui.txt root@192.168.21.129:/root/akui.txt
root@192.168.21.129‘s password:
sending incremental file list
akui.txt
sent 84 bytes received 35 bytes 238.00 bytes/sec
total size is 0 speedup is 0.00
[root@akuilinux01 sbin]# vim rsync.expect
#!/usr/bin/expect
set passwd "s5381561"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -avR --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
[root@akuilinux01 sbin]# vim /tmp/ip.list
192.168.21.129
127.0.0.1
[root@akuilinux01 sbin]# vim /tmp/file.list
/root/222/1111/11.txt
/root/1.txt
[root@akuilinux01 sbin]# vim rsync.sh
#!/bin/bash
for ip in `cat /tmp/ip.list`
do
echo $ip
./rsync.expect $ip /tmp/file.list
done
root@akuilinux01 sbin]# sh rsync.sh
192.168.21.129
spawn rsync -avR --files-from=/tmp/file.list / root@192.168.21.129:/
root@192.168.21.129‘s password:
building file list ... done
root/
root/1.txt
root/222/
root/222/1111/
root/222/1111/11.txt
sent 299 bytes received 63 bytes 724.00 bytes/sec
total size is 72 speedup is 0.20
127.0.0.1
spawn rsync -avR --files-from=/tmp/file.list / root@127.0.0.1:/
The authenticity of host ‘127.0.0.1 (127.0.0.1)‘ can‘t be established.
ECDSA key fingerprint is SHA256:r2y+hZVvvomXE4d3uRSM0cO+kMdqyWAOMqpTtA2qp6I.
ECDSA key fingerprint is MD5:ee:da:3b:ea:27:56:38:82:bc:e8:73:10:18:a0:c2:bd.
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.
root@127.0.0.1‘s password: [root@akuilinux01 sbin]#
[root@akuilinux01 sbin]# vim exe.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "s5381561"
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"
[root@akuilinux01 sbin]# vim exe.sh
#!/bin/bash
for ip in `cat /tmp/ip.list`
do
echo $ip
./exe.expect $ip "w;ls /tmp"
done
[root@akuilinux01 sbin]# chmod a+x exe.expect
[root@akuilinux01 sbin]# sh exe.sh
192.168.21.129
spawn ssh root@192.168.21.129
root@192.168.21.129‘s password:
Last login: Sun Jul 22 22:19:51 2018 from 192.168.21.128
[root@akuilinux02 ~]# w;ls /tmp
23:34:18 up 1:56, 2 users, load average: 0.08, 0.03, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.21.1 22:14 27:06 0.01s 0.01s -bash
root pts/1 192.168.21.128 23:34 0.00s 0.01s 0.00s w
12.txt
mysql.sock
systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-chronyd.service-5DHBEF
systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-vgauthd.service-sG39NN
systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-vmtoolsd.service-CVypi4
[root@akuilinux02 ~]# 127.0.0.1
spawn ssh root@127.0.0.1
root@127.0.0.1‘s password:
Last failed login: Sun Jul 22 23:28:56 CST 2018 from 127.0.0.1 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Sun Jul 22 21:35:11 2018 from 192.168.21.1
[root@akuilinux01 ~]# w;ls /tmp
23:34:18 up 2:05, 2 users, load average: 0.12, 0.05, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.21.1 21:35 2.00s 0.25s 0.00s /usr/bin/expect ./exe.expect 127.
root pts/1 127.0.0.1 23:34 0.00s 0.00s 0.00s w
12.txt
akuilinux
aming.sock
file.list
ip.list
lalal
mysql.sock
systemd-private-09ebb5bc0a88443db92bc329d77b2efc-chronyd.service-WDveS2
systemd-private-09ebb5bc0a88443db92bc329d77b2efc-httpd.service-Ok7bke
systemd-private-09ebb5bc0a88443db92bc329d77b2efc-vgauthd.service-B8RbhF
systemd-private-09ebb5bc0a88443db92bc329d77b2efc-vmtoolsd.service-WiU5il
systemd-private-c27889f557f94d1b8cfed596ba761ba5-chronyd.service-kH7zvQ
systemd-private-c27889f557f94d1b8cfed596ba761ba5-httpd.service-Cj8ABZ
systemd-private-c27889f557f94d1b8cfed596ba761ba5-vgauthd.service-21n8Br
systemd-private-c27889f557f94d1b8cfed596ba761ba5-vmtoolsd.service-8VTbyC
www.sock
expect脚本同步文件,expect脚本指定host和要同步的文件,构建文件分发系统,批量远程执行
标签:rem 思路 HERE emd 定义 ast 命令行 tty 配置文件
原文地址:http://blog.51cto.com/akui2521/2148801