分发系统——expect

20.27 分发系统介绍

expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令。但当不能使用密钥验证的时候,我们就没有办法了。所以,这时候只要知道对方机器的账号和密码就可以通过expect脚本实现登录和远程命令。

分发准备:

模板脚本、服务器IP、用户名、密码、expect脚本

20.18 expect脚本远程登录

使用expect前需要先安装:

[root@z1 ~]# yum install -y expect
安装完成后就可以写expect脚本了。

自动远程登录,并执行命令

远程登录一台主机:
[root@z1 ~]# ssh 192.168.8.138
Are you sure you want to continue connecting (yes/no)? yes
[email protected]'s password:
Last login: Wed Sep 20 18:36:21 2017 from 192.168.8.1
#如果是第一次登录,则会提示(yes/no),如果之前登陆过则不会提示
#那么如何使其再次提示呢?

[root@z1 ~]# vim /root/.ssh/known_hosts
##编辑该文件,将其中内容清空即可。
expect远程登录脚本:

[root@z1 ~]# vim 1.expect
#! /usr/bin/expect
set host "192.168.8.138"
#连接到主机192.168.8.138
set passwd "123456"
#密码
spawn ssh root@$host
#spawn调用shell命令ssh(登录),“set host”和“set passwd”为expect定义的两个变量
expect {
"yes/no" { send "yes\r"; exp_continue}
#ssh首次远程登录一台主机是会提示yes/no;"\r“表示回车
"assword:" { send "$passwd\r" }
#密码
}
interact
#interact的作用是停留在远程机器上,不退出
#expect脚本结束符号:expect eof——执行结束后暂停几秒钟后退出
#如果不加任何结束符号,命令执行完后马上退出

更改文件权限:
[root@z1 ~]# chmod a+x 1.expect

执行该脚本:
[root@z1 ~]# ./1.expect
spawn ssh [email protected]
The authenticity of host '192.168.8.138 (192.168.8.138)' can't be established.
ECDSA key fingerprint is 79:63:d9:ee:35:ad:f3:41:8d:45:b0:3b:c2:53:f6:f2.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.8.138' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Wed Sep 20 18:45:04 2017 from 192.168.8.136
[root@z2 ~]#
即,远程登录到机器“[root@z2 ~]”。

20.29 expect脚本远程执行命令

[root@z1 ~]# vim 2.expect
#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh [email protected]

expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]"
#匹配到“]”时执行下面的命令
send "touch /tmp/12.txt\r"
expect "]
"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"

[root@z1 ~]# chmod a+x 2.expect

执行脚本:
[root@z1 ~]# ./2.expect
spawn ssh [email protected]
[email protected]'s password:
Last login: Wed Sep 20 18:51:51 2017 from 192.168.8.136
[root@z2 ~]# touch /tmp/12.txt
[root@z2 ~]# echo 1212 > /tmp/12.txt
[root@z2 ~]#
[root@z1 ~]#
执行完命令后退出root@z2。

检查执行结果:

[root@z2 ~]# cat /tmp/12.txt
1212
20.30 expect脚本传递参数

[root@z1 ~]# vim 3.expect
#!/usr/bin/expect
#调用expect内置变量:“[lindex $argv 0]”、“[lindex $argv 1]”、“[lindex $argv 2]”
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "123456"
set cm [lindex $argv 2]
spawn ssh $user@$host

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]"
send "$cm\r"
expect "]
"
send "exit\r"

更改权限:
[root@z1 ~]# chmod a+x 3.expect

执行脚本:
[root@z2 ~]# [root@z1 ~]# ./3.expect root 192.168.8.138 ls;w
spawn ssh [email protected]
[email protected]'s password:
Last login: Wed Sep 20 19:12:48 2017 from 192.168.8.136
[root@z2 ~]# ls
anaconda-ks.cfg
[root@z2 ~]# 19:14:04 up 41 min, 2 users, load average: 0.02, 0.02, 0.08
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 18:33 37:36 0.15s 0.15s -bash
root pts/0 192.168.8.1 18:36 4.00s 0.19s 0.01s w

猜你喜欢

转载自blog.51cto.com/13242922/2107956
今日推荐