expect脚本安装NetBackup

这是一个使用Expect脚本语言编写的自动化安装脚本,主要用于NetBackup客户端的安装准备。Expect是一种用于自动化交互式程序的工具,常用于需要用户输入的场景。

脚本功能概述

  1. 让用户选择安装位置(NDC或BDC)
  2. 根据选择设置不同的参数(location、token、ping_target)
  3. 检查目标服务器是否可达(ping测试)
  4. 创建安装目录
  5. 安装必要的依赖包
  6. 解压NetBackup安装包

脚本内容

#!/usr/bin/expect
set timeout -1
send_user "Please choose: NDC OR BDC: "
expect_user {
    "NDC" {
        set location "XSHPRDINFNBUOl"
        set token "ULTBLAFBMCFGXCSV"
        set ping_target "172.16.8.42"
        # 检查hosts文件中是否已有该IP的解析
        if {[catch {exec grep -q "^$ping_target" /etc/hosts} result]} {
            send_user "Adding $ping_target $location to /etc/hosts...\n"
            if {[catch {exec sh -c "echo '$ping_target $location' >> /etc/hosts"} result]} {
                send_user "Failed to update /etc/hosts: $result\n"
                exit 1
            }
        } else {
            send_user "$ping_target already exists in /etc/hosts\n"
        }
    }
    "BDC" {
        set location "BJnRPIyquoI"
        set token "VDSWMTKETBPZRPUE"
        set ping_target "172.16.16.17"
        # 检查hosts文件中是否已有该IP的解析
        if {[catch {exec grep -q "^$ping_target" /etc/hosts} result]} {
            send_user "Adding $ping_target $location to /etc/hosts...\n"
            if {[catch {exec sh -c "echo '$ping_target $location' >> /etc/hosts"} result]} {
                send_user "Failed to update /etc/hosts: $result\n"
                exit 1
            }
        } else {
            send_user "$ping_target already exists in /etc/hosts\n"
        }
    }
}

# Ping测试
if {[catch {exec ping -c 3 $ping_target} result]} {
    send_user "Ping $ping_target failed: $result\n"
    exit 1
}

# 创建目录
if {[catch {exec mkdir -p /data/app/Netbackup/} result]} {
    send_user "Failed to create directory: $result\n"
    exit 1
}

# 安装依赖包
if {[catch {exec dnf install -y libnsl libnsl.so.l} result]} {
    send_user "Failed to install package: $result\n"
    exit 1
}

# 解压安装包
if {[catch {exec tar -xzf /data/software/NetBackup_l0.l.l_CLIENTS2.tar.ga -C /data/app/Netbackup/} result]} {
    send_user "Failed to decompress compressed package: $result\n"
    exit 1
}

# 开始安装NBU
spawn /data/app/Netbackup/install
expect {
    "*y,n*" {
        sleep 2
        send "y\r"
        exp_continue
    }
    "*1,2*" {
        sleep 2
        send "2\r"
        exp_continue
    }
    "*NetBackup primary server*" {
        sleep 2
        send "$location\r"
        exp_continue
    }
    "*token for*" {
        send "$token\r"
        sleep 2
        exp_continue
    }
}
send_user "Preparation completed successfully!\n"

#查看NBU状态
/usr/openv/netbackup/bin/bpps -x

脚本详解

  1. 初始设置:

    • set timeout -1 设置无限等待时间
    • send_user 向用户显示提示信息
  2. 用户选择:

    • 使用expect_user等待用户输入"NDC"或"BDC"
    • 根据选择设置不同的变量值
  3. Ping测试:

    • 使用exec ping -c 3发送3个ping包测试目标服务器
    • catch 用于检测命令是否执行成功(类似 Shell 中的 if [ $? -eq 0 ])。
    • 如果命令执行成功(返回状态码 0),catch 返回 0result 存储命令的标准输出(stdout)。
    • 如果命令执行失败(返回非 0 状态码),catch 返回 1result 存储命令的错误输出(stderr)。
  4. 目录创建:

    • 使用mkdir -p创建目录(-p参数确保父目录不存在时也会创建)
  5. 依赖安装:

    • 使用dnf install -y自动安装依赖包(-y参数自动确认)
  6. 解压安装包:

    • 使用tar -xzf解压tar.gz压缩包到指定目录
  7. 安装NBU

脚本运行

1. 直接执行(推荐)

前提:确保脚本有可执行权限,且第一行 shebang 正确(#!/usr/bin/expect

# 赋予执行权限
chmod +x your_script.exp

# 直接运行
./your_script.exp

2. 通过 expect 命令执行

如果脚本没有可执行权限,或需显式指定解释器:

expect your_script.exp

3. 调试模式执行

如果需要排查问题,可以启用调试:

expect -d your_script.exp  # 显示详细执行过程

你可能感兴趣的:(Linux系统运维,linux)