当前位置:优学网  >  在线题库

Shell脚本基于条件匹配运行ssh,其中卷名与服务器名匹配

发表时间:2022-07-16 00:23:55 阅读:82

我有服务器列表CLS和卷列表作为一个数组,如array_fxn1,等等,我需要根据条件运行相同的命令:

因此,我需要基于条件匹配在所有服务器上运行相同的命令,如..

array_fxn1 == fxn101
array_fxn2 == fxn201
array_fxn3 == fxn301
array_fxn4 == fxn401

服务器列表如下:

CLS="""
fxn101 
fxn201 
fxn301 
fxn401 
fxn501 
"""

Below is the code:

#!/bin/bash
echo "Plese enter LDAP Project Name"
read -p "Enter LDAP Project Name:" ldap_proj
ldap_volList=$(cat archive_dashboard/ldap-project-nismap.csv | grep "$ldap_proj" | tr "," "\t"| awk '{print $NF}'| tr -d '"')
#
array_fxn1=$(echo "$ldap_volList" | grep ^fxn1)
array_fxn2=$(echo "$ldap_volList" | grep ^fxn2)
array_fxn3=$(echo "$ldap_volList" | grep ^fxn3)
array_fxn4=$(echo "$ldap_volList" | grep ^fxn4)
array_fxn6=$(echo "$ldap_volList" | grep ^fxn6)
array_fxn7=$(echo "$ldap_volList" | grep ^fxn7)
array_fxn8=$(echo "$ldap_volList" | grep ^fxn8)

if [ ! -z "$ldap_volList" ]; then
     for i in $array_fxn4;
     do
       volstyle=$(ssh fxn401 "row 0; vol show $i -fields volume-style-extended" |awk '/fxn/{print $3}')
       if [ $volstyle == "flexvol" ];then
         echo -n "$ldap_proj:$i  "
         ssh fxn401 "row 0;set -unit MB; \
         vol show-footprint $i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}'
       fi
     done

上面的脚本运行良好,并提供以下输出,但以这种方式,我需要基于卷阵列array_fxn1等为每个服务器定义每个if块:

**输出:***下面是脚本的正确输出.

$ bash hotvscold-test.sh
Plese enter LDAP Project Name
Enter LDAP Project Name:star_plus
star_plus:fxn4008_star_plus_dbspace6  206578MB 132736MB 63045MB
star_plus:fxn4008_star_plus_dbspace5  1133390MB 356483MB 762600MB
star_plus:fxn4008_star_plus_dbspace8  5148497MB 347456MB 4722360MB
star_plus:fxn4008_star_plus_dbspace7  7212141MB 2445281MB 4666809MB
star_plus:fxn4008_star_plus_dbspace6  6593136MB 243604MB 6219384MB
star_plus:fxn4008_star_plus_dbspace5  998278MB 67751MB 867004MB
star_plus:fxn4008_star_plus_dbspace4  2924361MB 204011MB 2587825MB

请帮助并提供专家建议.

🎖️ 优质答案
相关问题