shell编程实现免密登陆以及软件自动安装和配置

之前我写过ssh自动化免密登陆,然而在大数据集群搭建过程中每个节点需要安装许多软件并且需要配置相关文件,所以现在我们实现一个自动安装软件脚本。

代码:(安装软件使用局域网yum源我之前写过:https://blog.csdn.net/a602519773/article/details/81909730

这里我编写了两个shell脚本:ssh_copy和install.sh

脚本1:ssh_copy.sh

这个脚本中包含了两个方法,第一个是用于远程ssh操作,第二个是用来ssh每个节点;再下面的代码,首先使用expect实现交互式的执行auto_ssh(),然后在ssh_copy()中调用auto_ssh()实现多台主机免密,最后使用一个for循环将自动化安装脚本下发给各主机,并执行该脚本

#!/bin/bash
services="host02 host03"
password=hadoop
auto_ssh()
{
   expect -c "set timeout -1;
     spawn   ssh-copy-id $1;
   expect {
        "*(yes/no)*" {send yes\r; exp_continue;}
        "*(password*)"{send $2\r;exp_continue;}
        eof  {exit 0;}
    }";      
}

ssh_copy()
{
    for service in $services
    do
       auto_ssh() $service $password
    done
}
ssh_copy

for service in $services
do
    scp -r install.sh root@$service:/root
    ssh root@$service /root/install.sh
done

 

脚本2:install.sh

#!/bin/bash
base_server=host01
yum install -y wget
wget $base_server/soft/jdk-7u45-linux-x64.tar.gz
tar -zxvf jdk-7u45-linux-x64.tar.gz -C /usr/local
cat  >> /etc/profile << EOF
export JAVA_HOME=/usr/local/jdk1.7.0_45
export PATH=\$PATH:\$JAVA_HOME/bin
EOF

 

上面代码分为两部分,首先是使用局域网yum源下载wget下载命令工具,然后使用wget下载我们上传在yum源中的软件包,最后就是实现自动化配置,这里使用了文本输入重定向

eq :

[root@host01 soft]# echo 111 >> a.txt

[root@host01 soft]# cat a.txt

111

[root@host01 soft]# cat >> a.txt << EOF

> hello world

> EOF

[root@host01 soft]# cat a.txt

111

hello world

这里我们发现使用文本输入重定向,将输入的hello world追加到了a.txt文件中。

同理我们看上边代码,经过重定向我们将需要添加的配置信息:

export JAVA_HOME=/usr/local/jdk1.7.0_45
export PATH=\$PATH:\$JAVA_HOME/bin

追加到了/etc/profile配置文件中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值