阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

expect环境安装以及简单脚本测试

142次阅读
没有评论

共计 4663 个字符,预计需要花费 12 分钟才能阅读完成。

expect 是交互性很强的脚本语言,可以帮助运维人员实现批量管理成千上百台服务器操作,是一款很实用的批量部署工具
expect 依赖于 tcl,而 linux 系统里一般不自带安装 tcl,所以需要手动安装

下载:expect-5.43.0.tar 和 tcl8.4.11-src.tar

可以到 Linux 公社资源站下载:

—————————————— 分割线 ——————————————

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2017 年资料 / 2 月 /20 日 /expect 环境安装以及简单脚本测试 /

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

—————————————— 分割线 ——————————————

将 expect 和 tcl 的软件包下载放到 /usr/local/src 目录下

(1)解压 tcl,进入 tcl 解压目录,然后进入 unix 目录进行编译安装
[root@linuxidc src]# tar -zvxf tcl8.4.11-src.tar.gz
[root@linuxidc src]# cd tcl8.4.11/unix
[root@linuxidc unix]# ./configure
[root@linuxidc unix]# make && make

(2)安装 expect
[root@linuxidc src]# tar -zvxf expect-5.43.0.tar.gz
[root@linuxidc src]# cd expect-5.43.0
[root@linuxidc expect-5.43.0]# ./configure –with-tclinclude=/usr/local/src/tcl8.4.11/generic –with-tclconfig=/usr/local/lib/
[root@linuxidc expect-5.43.0]# make && make install

(3)安装完成后进行测试
[root@linuxidc ~]# expect
expect1.1>
expect1.1>

—————————————————————————————————-

下面结合 shell 脚本做简单测试:

例 1:
从本机自动登录到远程机器 192.168.1.200(端口是 22,密码是:PASSWORD)
登录到远程机器后做以下几个操作:
1)useradd wangshibo
2)mkdir /opt/test
3) exit 自动退出

[root@linuxidc tmp]# cat test-ssh.sh
#!/bin/bash
passwd=’PASSWORD’
/usr/local/bin/expect <<-EOF
set time 30
spawn ssh -p22 root@192.168.1.201
expect {
“*yes/no” {send “yes\r”; exp_continue}
“*password:” {send “$passwd\r”}
}
expect “*#”
send “useradd wangshibo\r”
expect “*#”
send “mkdir /opt/test\r”
expect “*#”
send “exit\r”
interact
expect eof
EOF

[root@linuxidc tmp]# sh test.sh
spawn ssh -p22 root@192.168.1.201
root@192.168.1.201’s password:
Last login: Fri Sep 23 16:21:20 2016 from 192.168.1.23
[root@vm-002 ~]# useradd wangshibo
[root@vm-002 ~]# mkdir /opt/test
[root@vm-002 ~]# [root@linuxidc tmp]#

*******************************************************************************************************

例 2:

我们在部署无密码访问时, 手工建立 ssh 互信需要好几个步骤,并且中途人工交互(输入密码等),如果机器数目多,则很繁琐!

下面方法用于自动化生成 authorized_keys, 免去了手工数据.

方法: 利用 expect 编写 sshkey.exp 在远程主机上生成 id_rsa, 并重定向到本地. 在利用 noscp.exp. 把文件复制到远程主机
为了节省自己的时间,可以写个 expect 自动化脚本,分享如下:

(1)
如上 expect 安装后的路径是:
[root@linuxidc ~]# which expect
/usr/local/bin/expect

(2)
做个 expect 执行文件的软件
[root@linuxidc ~]# ln -s /usr/local/bin/expect /usr/bin/expect
[root@linuxidc ~]# ll /usr/bin/expect

(3)
编写 expect 脚本:
———————————————————————————–
1)
[root@linuxidc ~]# cat sshkey.exp
#!/usr/bin/expect

#sshkey.exp

if {$argc<3} {
puts stderr “Usage: $argv0 host user passwd “
exit 1
}

set host [lindex $argv 0]
set user [lindex $argv 1]
set pwd [lindex $argv 2]

set timeout 30

#spawn ssh ${user}@${host} “rm -rf ~/.ssh/id_rsa*”
#
#expect {
# “*yes/no” {send “yes\r”; exp_continue}
# “*password:” {send “$pwd\r”; exp_continue}
#}

spawn ssh ${user}@${host} “ssh-keygen -t rsa”

expect {
“*yes/no” {send “yes\r”; exp_continue}
“*password:” {send “$pwd\r”; exp_continue}
“Enter file in which to save the key*” {send “\n\r”; exp_continue}
“Overwrite*” {send “y\n”; exp_continue}
“Enter passphrase (empty for no passphrase):” {send “\n\r”; exp_continue}
“Enter same passphrase again:” {send “\n\r”}
}

spawn ssh ${user}@${host} “cat ~/.ssh/id_rsa.pub”

expect {
“*yes/no” {send “yes\r”; exp_continue}
“*password:” {send “$pwd\r”}
}

expect eof

—————————————————————————————————-
2)
[root@linuxidc ~]# cat noscp.exp
#!/usr/bin/expect

#noscp.exp

if {$argc<4} {
puts stderr “Usage: $argv0 localfile remotefile user passwd “
exit 1
}

set localfile [lindex $argv 0]
set remotefile [lindex $argv 1]
set user [lindex $argv 2]
set pwd [lindex $argv 3]

set timeout 30

spawn scp ${localfile} ${user}@${remotefile}

expect {
“*yes/no” {send “yes\r”; exp_continue}
“*password:” {send “$pwd\r”}
}

expect eof

————————————————————————

[root@linuxidc ~]# chmod 755 sshkey.exp
[root@linuxidc ~]# chmod 755 noscp.exp

(4)
脚本说明
./sshkey.exp 主机名 用户名 密码 (在远程主机生成 id_rsa)
./noscp.exp 本地文件 远程路径 远程用户密码 (无密码拷贝文件)

(5)验证:
[root@linuxidc ~]# ./sshkey.exp 192.168.1.201 root PASSWORD |grep ssh-rsa >> ~/.ssh/authorized_keys
[root@linuxidc ~]# ./noscp.exp ~/.ssh/authorized_keys 192.168.1.201:~/.ssh root PASSWORD
spawn scp /root/.ssh/authorized_keys root@192.168.1.201:~/.ssh
root@192.168.1.201’s password:
authorized_keys

这样,就能无密码登陆了!
[root@linuxidc ~]# ssh 192.168.1.201
Last login: Fri Sep 23 18:33:21 2016 from 192.168.1.7
[root@vm-002 ~]#

————————————————————————–
如果是多台机器的话,可以结合 shell 脚本进行批量执行

[root@linuxidc ~]# cat /root/ip.list
192.168.1.100
192.168.1.101
192.168.1.102
192.168.1.103
192.168.1.104
……
……

[root@linuxidc ~]# cat sshkey.sh
#!/bin/bash
user=’root’
password=’PASSWORD’
for ip in `cat /root/ip.list`
do
/root/sshkey.exp $ip $user $password |grep ssh-rsa >> ~/.ssh/authorized_keys
/root/noscp.exp ~/.ssh/authorized_keys $user@$ip:~/.ssh root PASSWORD
done

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/140839.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计4663字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中