分发系统——expect
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分发系统——expect相关的知识,希望对你有一定的参考价值。
20.27 分发系统介绍expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令。但当不能使用密钥验证的时候,我们就没有办法了。所以,这时候只要知道对方机器的账号和密码就可以通过expect脚本实现登录和远程命令。
分发准备:
模板脚本、服务器IP、用户名、密码、expect脚本
20.18 expect脚本远程登录
使用expect前需要先安装:
[[email protected] ~]# yum install -y expect
安装完成后就可以写expect脚本了。
自动远程登录,并执行命令
远程登录一台主机:
[[email protected] ~]# 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),如果之前登陆过则不会提示
#那么如何使其再次提示呢?
[[email protected] ~]# vim /root/.ssh/known_hosts
##编辑该文件,将其中内容清空即可。
expect远程登录脚本:
[[email protected] ~]# vim 1.expect
#! /usr/bin/expect
set host "192.168.8.138"
#连接到主机192.168.8.138
set passwd "123456"
#密码
spawn ssh [email protected]$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——执行结束后暂停几秒钟后退出
#如果不加任何结束符号,命令执行完后马上退出
更改文件权限:
[[email protected] ~]# chmod a+x 1.expect
执行该脚本:
[[email protected] ~]# ./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
[[email protected] ~]#
即,远程登录到机器“[[email protected] ~]”。
20.29 expect脚本远程执行命令
[[email protected] ~]# 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"
[[email protected] ~]# chmod a+x 2.expect
执行脚本:
[[email protected] ~]# ./2.expect
spawn ssh [email protected]
[email protected]‘s password:
Last login: Wed Sep 20 18:51:51 2017 from 192.168.8.136
[[email protected] ~]# touch /tmp/12.txt
[[email protected] ~]# echo 1212 > /tmp/12.txt
[[email protected] ~]#
[[email protected] ~]#
执行完命令后退出[email protected]。
检查执行结果:
[[email protected] ~]# cat /tmp/12.txt
1212
20.30 expect脚本传递参数
[[email protected] ~]# 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 [email protected]$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]"
send "$cm\r"
expect "]"
send "exit\r"
更改权限:
[[email protected] ~]# chmod a+x 3.expect
执行脚本:
[[email protected] ~]# [[email protected] ~]# ./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
[[email protected] ~]# ls
anaconda-ks.cfg
[[email protected] ~]# 19:14:04 up 41 min, 2 users, load average: 0.02, 0.02, 0.08
USER TTY FROM [email protected] 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
以上是关于分发系统——expect的主要内容,如果未能解决你的问题,请参考以下文章
分发系统介绍expect脚本远程登录expect脚本远程执行命令expect脚本传递参数