使用场景
Expect 允许您自动登录远程机器并自动远程运行命令。当然,如果采用无密码密钥认证,还可以实现远程自动登录和自动执行命令。但是,如果您无法使用密钥身份验证,则无能为力。所以目前来说,如果你知道对方机器上的账号和密码,就可以通过expect脚本实现登录和远程命令。
安装
期待yum install -y 脚本
脚本1:自动远程登录
vim 1.期望
#! /usr/bin/expectset 主机'192.168.36.131'set passwd '123456'spawn ssh root@$hostexpect {'yes/no' { 发送'yes\r'}'assword:' { 发送'$passwd\ r' }}先互动清除之前的链接
添加/root/.ssh/known_hosts权限并运行即可实现这里的自动登录。
[root@cc-01 sbin]# chmod a+x 1.expect [root@cc-01 sbin]# ./1.expect spawn ssh root@192.168.36.131 信任主机“192.168.36.131 (192.168.36.131)”无法确定性别。 ECDSA 密钥指纹为SHA256:WwqBD7lw8dAc9G+m4coHW+7vYF/9Jw3rlo6cVNjXveM 0e8:49:1c:4b:60:15:7c:57: f1. 您想继续连接吗?(是/否) YesWarning: 已将“192.168.36.131”(ECDSA) 永久添加到列表中。已知主机的密码.root@192.168.36.131 : 上次登录: Sun Oct 7 01:17:08 2018 from 192.168.36.130[root@cc-02 ~]# 脚本2:自动远程登录后,运行命令并退出
vim 2.期望
#!/usr/bin/expectset 用户'root'set passwd '123456'spawn ssh $user@192.168.36.131expect {'yes/no' { 发送'yes\r' 'password:' { 发送'$passwd \r' }}expect ']*'send 'touch /tmp/12.txt\r'expect ']*'send 'echo 1212 /tmp/12.txt\r'expect ']*'send 'exit\r '嵌入
[root@cc-01 sbin]# chmod a+x 2.expect [root@cc-01 sbin]# ./2.expect spawn ssh root@192.168.36.131root@192.168.36.131的密码: 上次登录: Sunday 10 Mon 7th 01:20:02 2018 from 192.168.36.130[root@cc-02 ~]# touch /tmp/12.txt[root@cc-02 ~]# echo 1212 /tmp/12.txt 检测执行是否成功
脚本3:传递参数
vim 3. 期待
#!/usr/bin/expectset 用户[lindex $argv 0]set host [lindex $argv 1]set passwd '123456'set cm [lindex $argv 2]spawn ssh $user@$hostexpect {'是/否' { send 'yes\r'}'password:' { send '$passwd\r' }}expect ']*'send '$cm\r'expect ']*'send 'exit\r'执行
[root@cc-01 sbin]# chmod a+x 3.expect [root@cc-01 sbin]# ./3.expect root 192.168.36.131 lsspawn ssh root@192.168.36.131root@192.168.36.131: 的密码最后登录: Sun Oct 7 01:29:18 2018 从192.168.36.130[root@cc-02 ~]# lsanaconda-ks.cfg[root@cc-02 ~]# [root@cc-01 sbin]# 期待./3.root 192.168 待办事项。 36.131 'ls;w;vmstat 1 5'spawn ssh root@192.168.36.131root@192.168.36.131 的密码: 上次登录: 10 月7 日星期日01:36:54 从2018 年起192.168.36.130[ root@cc-02 ] #ls; vmstat 1 5anaconda-ks.cfg 01:37:45 最多3:24,3 个用户,平均负载: 0.00、0.01、0.05USER TTY 来自LOGIN@ IDLE JCPU PCPU WHATroot tty1 01:14 23:21 0.02s 0。 0 2s -bashroot pts/0 192.168.36.1 01:02 7:13 0.06 秒0.06秒-bashroot pts/1 192.168.36.130 01:37 0.00s 0.01s 0.00s wprocs -----------内存---------- ---交换-- -- -- -io ---- -system-- -------cpu---- r b swpd 空闲缓冲区高速缓存si so bi bo 在cs us sy id wa st 2 0 0 772812 2108 128168 0 0 9 1 58 66 0 0 100 0 0 0 0 0 772772 2108 128204 0 0 0 0 108 114 0 1 99 0 0 0 0 0 772772 2108 128204 0 0 0 0 106 106 0 0 100 0 0 0 0 0 772 2108 128204 0 0 0 0 88 85 0 0 100 0 0 0 0 0 772772 2108 128204 0 0 0 0 96 91 0 0 100 0 0[root@cc-02 ~]# [root@cc-01 sbin]# 脚本4:自动删除同步到的文件
vim 4. 期待
#!/usr/bin/expectset passwd '123456'spawn rsync -av root@192.168.36.131:/tmp/12.txt /tmp/expect {'是/否' { 发送'yes\r'}'password:' { 发送'$passwd\r' }}预计未来执行
[root@cc-01 sbin]# chmod a+x 4.expect [root@cc-01 sbin]# ./4.expect spawn rsync -av root@192.168.36.131:/tmp/12.txt /tmp/root@ 192.168.36.131 的密码: 接收增量文件list12.txt 已发送43 个字节已接收97 个字节280.00 个字节/段总大小为5 加速为0.04[root@cc-01 sbin]# cat /tmp/12.txt 1212expect eof;做吧。否则,该语句将不会被执行。
set timeout 设置超时时间。 -1是永久的
脚本5:指定要同步的主机和文件
vim 5. 期望
#!/usr/bin/expectset passwd '123456'set host [lindex $argv 0]set file [lindex $argv 1]spawn rsync -av $file root@$host:$fileexpect {'yes/no' { send 'yes \r'}'password:' { send '$passwd\r' }}希望被执行
[root@cc-01 sbin]# chmod a+x 5.expect [root@cc-01 sbin]# ./5.expect 192.168.36.131 '/tmp/12.txt'spawn rsync -av /tmp/12. root@192.168.36.131:/tmp/12.txtroot@192.168.36.131 的txt 密码: 发送增量文件列表已接收45 字节12 字节38.00 字节/段总大小为5 加速为0.09
构建文件分发系统
要求背景
大公司无疑会不时地更新他们的网站和配置文件,并且毫无疑问会使用大量的机器,从几台到几十台甚至几百台。因此,文件自动同步非常重要。
实施思路
首先,你需要有一台模板机,并准备好你要分发的文件。接下来使用expect脚本将需要同步的文件批量分发到目标机器上。
核心命令
rsync -av --files-from=list.txt/root@host:/
文件分发系统介绍
vimrsync.expect
#!/usr/bin/expectset passwd '123456'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 eofvim /tmp/ip.list 是需要同步的远程机器的IP
192.168.36.131127.0.0.1vim /tmp/list.txt 是同步文件的路径
/tmp/12.txt/root/shell/ip.sh/root/111/1.txtvim rsync.sh 执行脚本
#!/bin/bashfor ip in `cat /tmp/ip.list`do./rsync.expect $ip /tmp/list.txtdone 执行
[root@cc-01 sbin]# chmod a+x rsync.expect [root@cc-01 sbin]# sh -x rsync.sh++ cat /tmp/ip.list+ for ''cat /tmp/ip.list 中的ip `'+ ./rsync.expect 192.168.36.131 /tmp/list.txtspawn rsync -av --files-from=/tmp/list.txt/为root@192.168.36.131:/root@192.168.36.131 构建password: 文件列表。 rsync: link_stat '/root/111/1.txt' failed: 没有这样的文件或目录(2)doneroot/tmp/sent 已接收148 字节22 字节340.00 字节/sect 总大小为63 加速为0.37rsync 错误: 某些文件/属性未传输(请参阅之前的错误)(代码23)main.c(1178) [sender=3.1.2]+ for ip in '`cat /tmp/ip.list `'+ ./rsync.expect 127.0.0.1 /tmp/list.txtspawn rsync -av --files-from=/tmp/list.txt/root@127.0.0.1:/host '127.0.0.1 (127.0.0.1)' 无法建立真实性。 ECDSA 密钥指纹为SHA256:WwqBD7lw8dAc9G+m4coHW+7vYF/9Jw3rlo6cVNjXveM 336 0493 33601c:4b:60:15:7c:57:f1。您确定要继续连接吗?(是/否)。 yesWarning: 已将“127.0.0.1”(ECDSA) 永久添加到已知主机列表中。 @127.0.0.1 password: [root@cc-01 sbin]# 批量执行命令
vim 执行程序.expect
#!/usr/bin/expectset host [lindex $argv 0]set passwd '123456'set cm [lindex $argv 1]spawn ssh root@$hostexpect {'yes/no' { send 'yes\r'}'password: ' { send '$passwd\r' }}expect ']*'send '$cm\r'expect ']*'send 'exit\r'vim exe.sh
对于`cat /tmp/ip.list` 中的ip 运行echo $ip ./exe.expect $ip 'w;free -m;ls /tmp'done
[root@cc-01 sbin]# chmod a+x exe.expect [root@cc-01 sbin]# sh -x exe.sh ++ cat /tmp/ip.list+ for ''cat /tmp/ip 中的ip .list` '+ echo 192.168.36.131192.168.36.131+ ./exe.expect 192.168.36.131' W; ls /tmp'spawn ssh root@192.168.36.131root@192.18.36.3331313131313131 3 13131313131 0 10 月7 日星期日01:37:45 2018 至192.168.36.130[root@cc-02 ~]# w;free -m;ls /tmp 02:45:30 最多4:31,3 个用户,平均负载: 0.00, 0.01, 0.05USER T login@idle TY JCPU PCPU 什么root tty1 01:14 1:31m 0.02s 0.02s -bashroot pts/0 192.168.36.1 01:02 47:06 0.06s 0.06s -bashroot pts/1 192.168.36.130 6045 0.00s 0.00s 0.00s可用的可用共享缓冲区/缓存总数Mem: 974 92 753 7 127 732Swap: 2047 0 204712.txt systemd-private-69bd6363742e4d5fa8acdcda8e7e5678-chronyd.service-q1SZkI[root@cc-02 ~]# + for ip in '`cat /tmp /ip.list`+ echo 127.0.0。 0.1 + ./exe.expect 127.0.0.1 'w;free -m;ls /tmp'spawn ssh root@127.0.0.1 root@127.0.0.1 的密码: 上次登录失败: Sun Oct 7 02:36:22 CST 2018 从ssh:nottty 上的本地主机自上次成功登录以来,已有两次登录尝试失败。上次登录: 天01:59336035 2018 [root@cc -01 ~]# w; FREE -M; LS /TMP 02:45:30 30 4:32,4 个用户,平均负载: 0.00, 0.01, 0 .05 个用户tty 来自LOGIN@ IDLE JC P.U. PCPU WHATroot tty1 01:59 46:02 0.02s 0.02s -bashroot pts/0 192.168.36.1 01:02 2.00s 0.31s 0.00s /usr/bin/expect ./exe.expect 127.0 0.1 w;frroot 点/1 1 92.168 36.1 01:16 15:38 0.16秒0.16 秒-bashroot pts/3 localhost 02:45 0.00 秒0.01 秒0.00 秒(可用的空闲共享缓冲区/缓存总数)Mem: 974 140 460 7 373 660Swap: 2047 0 204712.txt systemd-private-8e8116 4 0829b4077883431f61a93c3f6-chronyd.service -2YJZoL1 。日志systemd-private-b00b7d3acc854463945334b653d81b01-chronyd.service-1zBY3Lcc.sock systemd-private-b0559a02f4d4465c83e8d26e8f9b5d77-chronyd.service-2CCL3Fip.list 47bd571 6cf4 86aafcf 7d9280c40412-chronyd.service-DcJiqslist.txt vmware-rootphp-fcgi。袜子[root@cc-01 ~]# [root@cc-01 sbin]#
版权声明:本文由今日头条转载,如有侵犯您的版权,请联系本站编辑删除。