【发布时间】:2019-05-14 22:04:22
【问题描述】:
我的脚本的目标是通过 ssh-copy-id 将我的 ssh 密钥部署到我的 100 个服务器。
为了实现这个目标,我使用了 3 个文件。
创建参数
#!/bin/bash
while read line;
do
IPSrv=`echo $line | cut -d":" -f1`
Login=`echo $line | cut -d":" -f2`
Passwd=`echo $line | cut -d":" -f3`
./deployssh.sh $IP $Login $Passwd
done < ServerList.txt
这个脚本允许我将 IP、Login 和 Passwd 作为参数传递给我的期望脚本。
服务器列表.txt
Name:@IP:RootLogin:Password
....
此文件包含名称、地址 IP、服务器的 Root 登录名和密码。
deployssh.sh
#!/usr/bin/expect -f
set IPSrv [lindex $argv 1]
set Login [lindex $argv 2]
set Passwd [lindex $argv 3]
spawn ssh-copy-id $IPSrv@$Login -o StrictHostKeyChecking=no
expect -re "password:"
send -- "$Passwd\r"
这个期望脚本应该将我的 ssh 密钥部署到 ServerList 文件中提到的所有服务器。不过暂时不能用。
我想知道我做错了什么,你能帮我解决这个问题吗?
【问题讨论】:
-
看看sexpect (Expect for Shells),您可以用它来编写Expect脚本,只使用shell代码。