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/
  • 相关阅读:
    3js深入
    01课js初接触;
    弥合对象/关系之间的鸿沟(一)
    Spiral Matrix——螺旋矩阵
    原来···是不是高手,看try cathch都能看出来···
    Web视频分享处理类库的设计
    每个开发人员现在应该下载的十种必备工具
    使用C#得到局域网内所有主机名,IP地址,MAC地址,使用C# 实现查看所有系统事件
    IIS 错误 :“NETWORK SERVICE does not have write access” 解决办法
    配置Url Remap时发生Parser Error的解决办法
  • 原文地址:https://www.cnblogs.com/at0x7c00/p/7966364.html
Copyright © 2011-2022 走看看