expect

循环

#!/bin/bash

list="Earth is the Home of Human! ";
for i in $list;
do
echo word is $i;
done
https://blog.csdn.net/u011156212/article/details/21074479

ssh-keygen -t rsa -P ''
-P表示密码,-P '' 就表示空密码,也可以不用-P参数,这样就要三车回车,用-P就一次回车。
该命令将在/root/.ssh目录下面产生一对密钥id_rsa和id_rsa.pub。

一般采用的ssh的rsa密钥:
id_rsa     私钥
id_rsa.pub 公钥
下述命令产生不同类型的密钥
ssh-keygen -t dsa
ssh-keygen -t rsa
ssh-keygen -t rsa1

把A机下的/root/.ssh/id_rsa.pub 复制到B机的 /root/.ssh/authorized_keys文件里,先要在B机上创建好 /root/.ssh 这个目录,用scp复制

[root@A ~]# scp /root/.ssh/id_rsa.pub root@192.168.1.181:/root/.ssh/authorized_keys
3.authorized_keys的权限要是600!!!
[root@B ~]# chmod 600 /root/.ssh/authorized_keys


下面进行集群环境初始化脚本的编写,通过ssh + expect + scp实现服务器集群自动化搭建

第一步,服务器准备
这里使用docker模拟几台服务器,分别命名为node2,node3,node4(使用镜像chenqionghe/Ubuntu,密码统一为88888888),生产环境为ip或host

docker run -d --name node2 -p 2223:22 chenqionghe/ubuntu
docker run -d --name node3 -p 2224:22 chenqionghe/ubuntu
docker run -d --name node4 -p 2225:22 chenqionghe/ubuntu

还得有一台主控制服务器node1,负责操作所有的服务器节点

docker run -d --name node1 -p 2222:22 
--link node2:node2 
--link node3:node3 
--link node4:node4 
chenqionghe/ubuntu

初始化完成后进入node1节点

ssh root@127.0.0.1 -p 2222

安装必须软件

apt-get install expect -y

创建存放脚本的目录~/env

mkdir -p ~/env && cd ~/env

这里先模拟一个简单的安装包scripts/install.sh,安装vim命令

mkdir scripts
cat > scripts/install.sh <<EOF
#!/bin/bash bash
apt-get install vim -y
EOF

创建机器列表配置文件,vim nodes.txt

node2
node3
node4

第二步 编写自动化脚本
1.无交互ssh-keygen生成密钥对脚本,vim ssh-keygen.exp

#!/usr/bin/expect
#set enter "
"
spawn ssh-keygen
expect {
        "*(/root/.ssh/id_rsa)" {send "
";exp_continue}
        "*(empty for no passphrase)" {send "
";exp_continue}
        "*again" {send "
"}
}
expect eof

2.无交互分发公钥脚本,vim send-sshkey.exp

#!/usr/bin/expect
if { $argc != 2 } {
 send_user "usage: expect send-sshkey.exp file host
"
 exit
}
#define var
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "88888888"
spawn ssh-copy-id -i $file -p 22 root@$host
expect {
        "yes/no" {send "yes
";exp_continue}
        "*password" {send "$password
"}
}
expect eof 

3.远程批量执行ssh脚本, vim mssh.sh

#!/bin/bash
Port=22
if [ $# -lt 1 ];then
    echo "Usage: `basename $0` command"
    exit
fi
echo $@
for v in `cat nodes.txt`
do
    ssh -t -p ${Port} root@${v} $@
    if [ $? -eq 0 ];then
        echo "执行成功:$v"
    else
        echo "执行失败:$v"
    fi
done

4.自动化部署脚本,vim auto-deploy.sh

#!/usr/bin/env bash
#机器列表
HostList=`cat nodes.txt`
#端口号
Port=22
# 1.无交互生成密钥对
if [ ! -f ~/.ssh/id_rsa.pub ];then
    expect ssh-keygen.exp
fi
# 2.无交互分发公密钥
for v in ${HostList}
do
    expect send-sshkey.exp ~/.ssh/id_rsa.pub ${v}
    if [ $? -eq 0 ];then
        echo "公钥-发送成功:$v"
    else
        echo "公钥-发送失败:$v"
    fi
done
# 3.分发脚本文件(安装软件包)
for v in ${HostList}
do
    scp -P ${Port} -rp ~/env/scripts root@${v}:~
    if [ $? -eq 0 ];then
        echo "scripts-发送成功:$v"
    else
        echo "scripts-发送失败:$v"
    fi
done
# 4.使用脚本文件安装
for v in ${HostList}
do
    ssh -t -p ${Port} root@${v} /bin/bash ~/scripts/install.sh
done

到这里所有的文件已经创建完成,整个env目录结构如下

├── auto-deploy.sh
├── mssh.sh
├── nodes.txt
├── scripts
│  └── install.sh
├── send-sshkey.exp
└── ssh-keygen.exp

第三步 执行自动化脚本查看效果

sh auto-deploy.sh

执行成功结果

spawn ssh-keygen
...
公钥-发送成功:node2
...
公钥-发送成功:node3
...
公钥-发送成功:node4
install.sh 100% 40 41.4KB/s 00:00
scripts-发送成功:node2
install.sh 100% 40 45.0KB/s 00:00
scripts-发送成功:node3
install.sh 100% 40 39.9KB/s 00:00
scripts-发送成功:node4
...
Connection to node4 closed.

出现上面的结果代表3个node节点已经初始化完成,拷贝公钥+安装软件成功!

我们再来执行一下远程命令脚本,查看所有服务器系统类型

sh mssh.sh "cat /etc/os-release|head -n 1"

执行结果如下

cat /etc/os-release|head -n 1
NAME="Ubuntu"
Connection to node2 closed.
执行成功:node2
NAME="Ubuntu"
Connection to node3 closed.
执行成功:node3
NAME="Ubuntu"
Connection to node4 closed.
执行成功:node4


其他例子

创建密钥

ssh-keygen -t dsa -f ~/.ssh/id_dsa -P ""
-t:指定密钥对的密码加密类型(rsa,dsa两种)
-f:指定密钥对文件的生成路径包含文件名
-P(大写):指定密钥对的密码


#!/usr/bin/expect
    set host_ip [lindex $argv 0]
    spawn ssh-copy-id -i /root/.ssh/id_rsa.pub   $host_ip
    expect {
            -timeout 60
            "(yes/no)?" { send "yes
";exp_continue}
            "password:" { send "123456
"}
            timeout {puts "Connect timeout!";return}
    }
    expect eof
    exit -onexit {
            send_user "Job has finished!"
    }
    
    
    
 #!/bin/bash
    iplist=(
            192.168.0.241
            192.168.0.242
            192.168.0.243
    )
    for ip in ${iplist[*]};do
            expect /root/ssh164.exp $ip
    done    

原文地址:https://www.cnblogs.com/g2thend/p/12492958.html