zoukankan      html  css  js  c++  java
  • Shell编程实践之批量安装JDK

    实验环境

     只有两台机子,一台虚拟机192.168.1.200,另外一台物理机192.168.1.201。

    目标

    执行一个shell脚本,给这两台机子自动安装jdk。

    实验步骤

    1.自动设置ssh免密码登录

    执行脚本之后,遍历服务器ip,对每一个服务进行如下操作

    2.每台服务器自动从数据服务器(http服务器,提供jdk安装包下载)上下载jdk

    3.下载jdk后进行解压,并设置环境遍历

    关键点

    1.设置ssh免密码登录时需要输入密码,这里使用expect来自动输入密码:

    复制代码
    auto_ssh_copy_id() {
        expect -c "set timeout -1;
            spawn ssh-copy-id $1;  #$1表示传入函数的第一个参数:服务器ip地址
            expect {
                *(yes/no)* {send -- yes
    ;exp_continue;}  
                *assword:* {send -- $2
    ;exp_continue;} #服务器登录密码
                eof        {exit 0;}
            }";
    }
    复制代码

    2.登录到服务器上后自动执行脚本:

    复制代码
    for SERVER in $SERVERS
    do
        scp install.sh root@$SERVER:/root
        ssh root@$SERVER /root/install.sh
    done
    复制代码

    3.追加内容到某一个文件

    cat >> /etc/profile << EOF
    export JAVA_HOME=/usr/local/jdk1.7.0_45
    export PATH=$PATH:$JAVA_HOME/bin
    EOF

    > & >>的区别

    1.当文件不存在是> 和>>都可以自动生成文件;

    2.如果文件存在,>表示覆盖,>>则是追加

    这里使用了重定向来将内容追加打了/etc/profile中。

    cat >> /etc/profile 是一个命令,是说要把cat得到的内容追加到后面的文件中,那么cat的是什么内容呢?
    1.可以是某个文件,可以在后面指定 例如:cat >> /etc/profile/ a.txt
    2.如果后面不指定文件,那cat的输入流默认的就是键盘,可以用ctrl+c或ctrl+d来结束输入:
    复制代码
    [root@centos01 tmp]# cat > 1.txt
    a
    b
    c
    ^C
    [root@centos01 tmp]# 
    复制代码

      3.可以用另外一个输入流(<或<<)来对接到这个输入流上:多行的输入需要一个开始和结束标识:

    复制代码
    [root@centos01 tmp]# cat > 1.txt <<MARK
    > hello
    > how are you?
    > MARK
    [root@centos01 tmp]# cat 1.txt
    hello
    how are you?
    [root@centos01 tmp]# 
    复制代码

    boot.sh

    复制代码
    #!/bin/bash
    

    SERVERS="node-3.itcast.cn node-4.itcast.cn"
    PASSWORD
    =123456
    BASE_SERVER
    =172.16.203.100

    auto_ssh_copy_id() {
    expect -c "set timeout -1;
    spawn ssh-copy-id $1;
    expect {
    (yes/no) {send -- yes ;exp_continue;}
    assword: {send -- $2 ;exp_continue;}
    eof {exit
    0;}
    }
    ";
    }

    ssh_copy_id_to_all() {
    for SERVER in $SERVERS
    do
    auto_ssh_copy_id $SERVER $PASSWORD
    done
    }

    ssh_copy_id_to_all

    for SERVER in $SERVERS
    do
    scp install.sh root@$SERVER:/root
    ssh root@$SERVER /root/install.sh
    done

    复制代码

    install.sh

    复制代码
    #!/bin/bash
    

    BASE_SERVER=172.16.203.100
    yum install -y wget
    wget $BASE_SERVER/soft/jdk-7u80-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_80
    export PATH
    =$PATH:$JAVA_HOME/bin
    EOF

    复制代码

    安装httpd服务

    在192.168.1.200上安装httpd,并准备好jdk文件。

    yum -y install httpd
    复制代码
    [root@demo ~]# cd /var/www/
    [root@demo www]# ll
    drwxr-xr-x. 2 root root 4096 10月 20 00:40 cgi-bin
    drwxr-xr-x. 3 root root 4096 12月  4 00:36 error
    drwxr-xr-x. 2 root root 4096 10月 20 00:40 html
    drwxr-xr-x. 3 root root 4096 12月  4 00:36 icons
    [root@demo www]# cd html/
    [root@demo html]# mkdir soft
    [root@demo html]# ll
    drwxr-xr-x. 2 root root 4096 12月  4 00:39 soft
    复制代码
    sftp> put E:/BaiduNetdiskDownload/jdk-7u80-linux-x64.tar.gz
    [root@demo ~]# mv jdk-7u80-linux-x64.tar.gz /var/www/html/soft/
  • 相关阅读:
    异步编程
    写代码写至最有面向对象味道
    GitHub上整理
    用CQRS+ES实现DDD
    前端开发
    让低版本的IE浏览器 强制渲染为IE8 或者 以上 浏览器模式
    NHibernate系列
    hadoop搭建开发环境及编写Hello World
    Linux date -s(转)
    即时编译和打包您的 Groovy 脚本(转)
  • 原文地址:https://www.cnblogs.com/jpfss/p/9717006.html
Copyright © 2011-2022 走看看