❶ shell 腳本。自動安裝軟體,遇到要輸入yes怎麼自動輸入,不用人為介入
使用expect 下的spawn命令:
如:
expect << EOF
set timeout 5
spawn ssh -l $hostuser $hostaddr
expect {
"yes/no)?" { send "yes\r";exp_continue }
"assword:" { send "$passwd\r" }
}
expect "$hostuser*"
send "ssh-keygen -t rsa\r"
expect "Enter file in which to save the key*"
send "\r"
expect {
"(y/n)?" { send "n\r";exp_continue }
"Enter passphrase*" { send "\r";exp_continue }
"Enter same passphrase again:" { send "\r" }
}
interact
EOF
❷ linux下編寫一個shell腳本,實現自動安裝軟體
Linux下Oracle的自動安裝腳本,基本上就是yes和no
分享一個Linux下安裝ASM+ORACLE的自動化腳本
http://blog.csdn.net/johnlongyuan/article/details/48416019
❸ 用shell腳本怎麼實現安裝升級版本
A:本腳本運行的機器,Linux
B:待安裝JDK的機器, Linux
首先在腳本運行的機器A上確定可以ssh無密碼登錄到待安裝jdk的機器B上,然後就可以在A上運行本腳本:
復制代碼 代碼如下:
$ ./install-jdk.sh B的IP
or:
復制代碼 代碼如下:
$ ./install-jdk.sh "B的IP" "JDK的URI"
就可以在機器B上安裝JDK。jdk使用的tar包需要用戶自己設定DEFAULT_JDK_SRC=?,保證可以wget得到即可。下面是全部腳本內容:
復制代碼 代碼如下:
#!/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 [email protected] 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> $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 '#!{{[email protected]==>' >> /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 '#!<[email protected]}}'>> /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