自动远程登录,并执行命令
[root@localhost sbin]# cat 1.expect
#! /usr/bin/expect
set host “10.1.1.135”
set passwd “123456”
spawn ssh root@KaTeX parse error: Can't use function '\r' in math mode at position 35: …no" { send "yes\̲r̲"; exp_continue…passwd\r" }
}
interact
#结束
自动远程登录后,执行参数并退出
[root@localhost sbin]# cat 2.expect
#!/usr/bin/expect
set user “root”
set passwd “123456”
spawn ssh KaTeX parse error: Can't use function '\r' in math mode at position 46: …no" { send "yes\̲r̲"; exp_continue…passwd\r" }
}
expect “]"
send “touch /tmp/12.txt\r”
expect "]”
send “echo 1212 > /tmp/12.txt\r”
expect “]*”
send “exit\r”
[root@allen tmp]# ls
12.txt
ks-script-Mb3Fcv
yum.log
[root@localhost tmp]# cat 12.txt
1212
远程登录 并传递参数
[root@allen ~]# [root@localhost tmp]# cat 3.expect
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd “123456”
set cm [lindex $argv 2]
spawn ssh
u
s
e
r
@
user@
user@host
expect {
“yes/no” { send “yes\r”}
“password:” { send “KaTeX parse error: Can't use function '\r' in math mode at position 7: passwd\̲r̲" } } expect "]…cm\r”
set timeout -1
#-1永不超时 为5时,显示5秒
expect “]*”
send “exit\r”
[root@localhost tmp]# ./3.expect root 10.1.1.135 ls
spawn ssh root@10.1.1.135
root@10.1.1.135’s password:
Last login: Fri Sep 20 09:32:23 2019 from admin.innet.com
[root@allen ~]# ls
anaconda-ks.cfg
[root@allen ~]# [root@localhost tmp]# ./3.expect root 10.1.1.135 “ls;w”
spawn ssh root@10.1.1.135
root@10.1.1.135’s password:
Last login: Fri Sep 20 09:49:26 2019 from 10.1.1.137
[root@allen ~]# ls;w
anaconda-ks.cfg
09:50:16 up 18 min, 2 users, load average: 0.00, 0.01, 0.04
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 admin.innet.com 09:32 13:44 0.04s 0.04s -bash
root pts/1 10.1.1.137 09:50 0.00s 0.07s 0.04s w
自动同步文件
[root@localhost tmp]# cat 4.expect
#!/usr/bin/expect
set passwd “123456”
spawn rsync -av root@10.1.1.135:/tmp/12.txt /tmp/
#spawn是进入expect环境后才可以执行的expect内部命令,如果没有装expect或者直接在默认的SHELL下执行是找不到spawn命令的。
#远程登录10.1.1.135服务器后,将/tmp/下的12.txt文件同步到本机tmp文件夹下
expect {
“yes/no” { send “yes\r”}
“password:” { send “$passwd\r” }
}
expect eof
[root@localhost tmp]# ./4.expect
spawn rsync -av root@10.1.1.135:/tmp/12.txt /tmp/
root@10.1.1.135’s password:
receiving incremental file list
12.txt
sent 43 bytes received 97 bytes 21.54 bytes/sec
total size is 5 speedup is 0.04
[root@localhost tmp]# ls
12.txt
指定host和要同步的文件
[root@localhost tmp]# cat 5.expect
#!/usr/bin/expect
set passwd “123456”
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av
f
i
l
e
r
o
o
t
@
file root@
fileroot@host:KaTeX parse error: Can't use function '\r' in math mode at position 35: …no" { send "yes\̲r̲"} "password:" …passwd\r" }
}
expect eof
[root@localhost tmp]# cat 12.txt
123456
[root@allen tmp]# cat 12.txt
123456
[root@localhost tmp]# echo 456789 >> 12.txt
[root@localhost tmp]# ./5.expect 10.1.1.135 “/tmp/12.txt”
spawn rsync -av /tmp/12.txt root@10.1.1.135:/tmp/12.txt
root@10.1.1.135’s password:
sending incremental file list
12.txt
sent 105 bytes received 41 bytes 97.33 bytes/sec
total size is 14 speedup is 0.10
[root@allen tmp]# cat 12.txt
123456
456789
文件分发系统的实现
[root@localhost tmp]# cat file.list
/tmp/12.txt
/tmp/3.expect
/tmp/4.expect
/tmp/5.expect
/root/NBA.txt
#需要分发的文件及路径
[root@localhost tmp]# cat ip.list
10.1.1.135
10.1.1.137
#需要分发的服务器
[root@localhost tmp]# cat rsync.expect
#!/usr/bin/expect
set passwd “123456”
set host [lindex $argv 0]
set file [lindex
a
r
g
v
1
]
s
p
a
w
n
r
s
y
n
c
−
a
v
R
−
−
f
i
l
e
s
−
f
r
o
m
=
argv 1] spawn rsync -avR --files-from=
argv1]spawnrsync−avR−−files−from=file / root@KaTeX parse error: Expected 'EOF', got '#' at position 8: host:/ #̲核心命令,将本机器需要同步的文…passwd\r" }
}
expect eof
[root@localhost tmp]# cat rsync.sh
#!/bin/bash
for ip in cat /tmp/ip.list
do
./rsync.expect $ip /tmp/file.list
done
[root@localhost tmp]# sh -x rsync.sh
++ cat /tmp/ip.list
- for ip in ‘
cat /tmp/ip.list
’ - ./rsync.expect 10.1.1.135 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / root@10.1.1.135:/
root@10.1.1.135’s password:
building file list … done
root/
root/NBA.txt
tmp/
tmp/3.expect
tmp/4.expect
tmp/5.expect
sent 1,015 bytes received 98 bytes 742.00 bytes/sec
total size is 665 speedup is 0.60
- for ip in ‘
cat /tmp/ip.list
’ - ./rsync.expect 10.1.1.137 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / root@10.1.1.137:/
The authenticity of host ‘10.1.1.137 (10.1.1.137)’ can’t be established.
ECDSA key fingerprint is SHA256:GJUywd73Getq0aq3EijEmCwGJui/MsNvO3yki71u1rE.
ECDSA key fingerprint is MD5:53:31:45:50:3f:89:91:1e:cc:d7:8b:4b:42:79:bd:f4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘10.1.1.137’ (ECDSA) to the list of known hosts.
[root@localhost tmp]# ls
12.txt
3.expect
4.expect
5.expect
file.list
ip.list
ks-script-ynycYW
rsync.expect
rsync.sh
[root@allen tmp]# ls
123456
12.txt
3.expect
4.expect
5.expect
ks-script-Mb3Fcv
[root@allen /]# cd root
[root@allen ~]# ls
anaconda-ks.cfg NBA.txt
分发系统,命令批量执行
[root@localhost ~]# [root@localhost tmp]# cat exe.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd “123456”
set cm [lindex
a
r
g
v
1
]
s
p
a
w
n
s
s
h
r
o
o
t
@
argv 1] spawn ssh root@
argv1]spawnsshroot@host
expect {
“yes/no” { send “yes\r”}
“password:” { send “KaTeX parse error: Can't use function '\r' in math mode at position 7: passwd\̲r̲" } } expect "]…cm\r”
expect “]*”
send “exit\r”
[root@localhost tmp]# cat exe.sh
#!/bin/bash
for ip in cat /tmp/ip.list
do
./exe.expect $ip “hostname”
done
[root@localhost tmp]# ./exe.sh
spawn ssh root@10.1.1.135
root@10.1.1.135’s password:
Last login: Mon Sep 23 17:43:25 2019 from 10.1.1.137
[root@allen ~]# hostname
allen.localdomain
[root@allen ~]# spawn ssh root@10.1.1.137
root@10.1.1.137’s password:
Last failed login: Mon Sep 23 20:12:03 CST 2019 from 10.1.1.137 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Mon Sep 23 17:10:10 2019 from 10.1.1.112
[root@localhost ~]# hostname
localhost.localdomain