发布时间:2015-02-02 18:32:55作者:知识屋
A:本脚本运行的机器,Linux
B:待安装JDK的机器, Linux
首先在脚本运行的机器A上确定可以ssh无密码登录到待安装jdk的机器B上,然后就可以在A上运行本脚本:
$ ./install-jdk.sh B的IP
or:
$ ./install-jdk.sh "B的IP" "JDK的URI"
#!/bin/bash## @file# install-jdk.sh## @date# 2013-12-19## @author# cheungmine## @version# 0.0.1pre## @usage:# ./install-jdk.sh 192.168.122.206#################################################################################. common.sh#***********************************************************# install_jdk# install jdk on machine: /usr/local/lib## Parameters:# machine - root@ipaddr# jdkUri - uri for fetching tarball## Example:## install_jdk root@192.168.122.206 ftp://vm-ftp/pub/tarball/jdk-7u67-linux-x64.tar.gz##***********************************************************. common.sh# YOU MIGHT CHANGE BELOW LINE TO GET YOUR JDK TARBALL:DEFAULT_JDK_SRC="ftp://vm-ftp/pub/tarball/jdk-7u67-linux-x64.tar.gz"# DO NOT CHANGE BELOW TWO LINES:INSTALL_DIR="/usr/local/lib/java"LOCAL_DIR="./.tmp"function install_jdk() { echo -e "<INFO> install jdk on machine: $1" local DEST_LOGIN=$1 local JDK_URI=$2 local TAR=$(basename $JDK_URI) echo -e "<INFO> jdk: '$JDK_URI'" wget -c $JDK_URI -P $LOCAL_DIR -O $LOCAL_DIR/$TAR $(is_empty_dir "$LOCAL_DIR/jdk_untar") local ret=$? case $ret in $DIR_NOT_EXISTED) mkdir -p $LOCAL_DIR/jdk_untar ;; $DIR_IS_EMPTY) ;; $DIR_NOT_EMPTY) rm -rf $LOCAL_DIR/jdk_untar/* ;; *) exit $ERR_FATAL_ERROR ;; esac # untar to jdk_untar tar -zxf $LOCAL_DIR/$TAR -C $LOCAL_DIR/jdk_untar $(is_empty_dir "$LOCAL_DIR/jdk_untar") local ret=$? if [ "$ret" -eq "$DIR_NOT_EMPTY" ]; then local jdk_home=`ls $LOCAL_DIR/jdk_untar 2>/dev/null` echo $jdk_home else exit $ERR_FATAL_ERROR fi echo -e "<INFO> create folder on: $DEST_LOGIN:$INSTALL_DIR" local ret=`ssh $DEST_LOGIN "mkdir $INSTALL_DIR"` echo -e "<INFO> copy $jdk_home/ to: $DEST_LOGIN:$INSTALL_DIR/" local ret=`scp -r $LOCAL_DIR/jdk_untar/$jdk_home $DEST_LOGIN:$INSTALL_DIR` # remove local tar rm -rf $LOCAL_DIR/jdk_untar local DEST_JAVA_HOME=$INSTALL_DIR/$jdk_home echo -e "<TODO> remove old settings for install_jdk in /etc/profile" echo -e "<INFO> set /etc/profile: JAVA_HOME=$DEST_JAVA_HOME" local ret=`ssh $DEST_LOGIN "echo '' >> /etc/profile"` local ret=`ssh $DEST_LOGIN "echo '#!{{install_jdk@hgdb.net==>' >> /etc/profile"` local ret=`ssh $DEST_LOGIN "echo 'export JAVA_HOME=$DEST_JAVA_HOME' >> /etc/profile"` local ret=`ssh $DEST_LOGIN "echo 'export CLASSPATH=.://$JAVA_HOME/lib/tools.jar://$JAVA_HOME/lib/dt.jar' >> /etc/profile"` local ret=`ssh $DEST_LOGIN "echo 'export PATH=//$JAVA_HOME/bin://$JAVA_HOME/jre/bin://$PATH' >> /etc/profile"` local ret=`ssh $DEST_LOGIN "echo '#!<==install_jdk@hgdb.net}}'>> /etc/profile"` local ret=`ssh $DEST_LOGIN ". /etc/profile"`}function uninstall_jdk() { echo -e "<TODO> uninstall jdk from: $1"}#=======================================================================# ---- main() ----if [ -n $1 ]; then DEST_IP=$1 JDK_SRC=$DEFAULT_JDK_SRC if [ $# == 2 ]; then JDK_SRC=$2 fi echo -e "<INFO> install jdk on '$DEST_IP', jdk: '$JDK_SRC'" install_jdk "root@$DEST_IP" "$JDK_SRC"fi
#!/bin/bash## @file# common.sh## @date# 2013-12-18## @author# cheungmine## @version# 0.0.1pre########################################################################## Error Codes:#ERR_NOERROR=0ERR_FATAL_ERROR=-100# current user is not a rootERR_NOT_ROOT=100# file is already existedERR_FILE_EXISTED=200#***********************************************************# chk_root# check if is a root user## Example:# chk_root#***********************************************************function chk_root() { if [ $UID -ne 0 ]; then echo -e "<chk_root> current user is not a root./n" / " Because you will run all the steps from this shell with root" / "privileges,/n" / " you must become root right by typing:/n" / " $ sudo su" exit $ERR_NOT_ROOT fi}#***********************************************************# read_cfg# read section and key from ini file## Example:# value=$(read_cfg $CFG_FILE $SEC_NAME 'key')#***********************************************************function read_cfg() { CFGFILE=$1 SECTION=$2 KEYNAME=$3 RETVAL=`awk -F '=' '//['$SECTION'/]/{a=1}a==1&&$1~/'$KEYNAME'/{print $2;exit}' $CFGFILE` echo $RETVAL | tr -d '"'}#***********************************************************# write_cfg# write section and key to ini file## Example:# $(write_cfg $CFG_FILE 'secname' 'key' $value)#***********************************************************function write_cfg() { CFGFILE=$1 SECTION=$2 KEYNAME=$3 NEWVAL=$4 RETVAL=`sed -i "/^/[$SECTION/]/,/^/[/ {/^/[$SECTION/]/b;/^/[/b;s/^$KEYNAME*=.*/$KEYNAME=$NEWVAL/g;}" $CFGFILE` echo $RETVAL}#***********************************************************# real_path# get real path of given relative path## Example:# ABS_PATH=$(real_path $(dirname $0))#***********************************************************function real_path() { /cd "$1" /bin/pwd}#***********************************************************# copy_file# copy source file to destigation file without overwriting## Example:# copy_file $file1 $file2#***********************************************************function copy_file() { src=$1 dst=$2 if [ -a $dst ] then echo -e "<copy_file> file is already existed: '$dst'" exit $ERR_FILE_EXISTED fi echo -e "<copy_file> '$src' =>/n '$dst'" dstdir=$(dirname $dst) if [ -d $dstdir ] then : else mkdir -p $dstdir fi cp -p $src $dst}#***********************************************************# is_empty_dir# if a dir is empty# Returns:# 0 - not empty dir# 1 - is empty dir# 2 - dir not existed# Example:# is_empty_dir ~/workspace# echo $?## $(is_empty_dir './'# echo $?#***********************************************************DIR_NOT_EXISTED=2DIR_IS_EMPTY=1DIR_NOT_EMPTY=0function is_empty_dir() { local olddir=$PWD local indir=$1 if [ ! -d "$indir" ]; then return $DIR_NOT_EXISTED fi cd $indir local files=`ls 2>/dev/null` cd $olddir if [ -n "$files" ]; then return $DIR_NOT_EMPTY else return $DIR_IS_EMPTY fi}
linux一键安装web环境全攻略 在linux系统中怎么一键安装web环境方法
Linux网络基本网络配置方法介绍 如何配置Linux系统的网络方法
Linux下DNS服务器搭建详解 Linux下搭建DNS服务器和配置文件
对Linux进行详细的性能监控的方法 Linux 系统性能监控命令详解
linux系统root密码忘了怎么办 linux忘记root密码后找回密码的方法
Linux基本命令有哪些 Linux系统常用操作命令有哪些
Linux必学的网络操作命令 linux网络操作相关命令汇总
linux系统从入侵到提权的详细过程 linux入侵提权服务器方法技巧
linux系统怎么用命令切换用户登录 Linux切换用户的命令是什么
在linux中添加普通新用户登录 如何在Linux中添加一个新的用户
2012-07-10
CentOS 6.3安装(详细图解教程)
Linux怎么查看网卡驱动?Linux下查看网卡的驱动程序
centos修改主机名命令
Ubuntu或UbuntuKyKin14.04Unity桌面风格与Gnome桌面风格的切换
FEDORA 17中设置TIGERVNC远程访问
StartOS 5.0相关介绍,新型的Linux系统!
解决vSphere Client登录linux版vCenter失败
LINUX最新提权 Exploits Linux Kernel <= 2.6.37
nginx在网站中的7层转发功能