【问题标题】:Shell script for searching different on multiple servers用于在多个服务器上搜索不同的 Shell 脚本
【发布时间】:2017-02-26 14:44:24
【问题描述】:

我想在 Solaris 10 的多个服务器中搜索不同的包。一个文件包含包信息,另一个文件包含服务器信息。

我试过这个:

bash-3.00# cat pk
"VRTSvcs|VRTSvxfen"
"SUNWfmd|SUNWfsmgtr"

bash-3.00# cat ser
mokshi
niki

这是我的脚本:

bash-3.00# cat tt
#!/usr/bin/bash
>output
for j in `cat ser`
do
for ip in `cat pk`
do

M=`ssh $j  "pkginfo |egrep $ip |cut -d \" \" -f7 "`;
echo "$j " >>output
echo "$M" >>output
done
done

预期的输出是

cat output
bash-3.00# cat output

moksha

VRTSvcs
VRTSvcsag
VRTSvcsea
VRTSvxfen

niki

SUNWfmd
SUNWfmdr
SUNWfsmgtr

但是当我运行脚本时,它会像这样运行两次:

bash-3.00# bash -x tt

++ cat ser

+ for j in '`cat ser`
'
++ cat pk

+ for ip in '`cat pk`'
++ ssh mokshi 'pkginfo |egrep "VRTSvcs|VRTSvxfen" |cut -d " " -f7 '
Password:
+ M='VRTSvcs
VRTSvcsag
VRTSvcsea
VRTSvxfen'
+ echo 'mokshi '
+ echo 'VRTSvcs
VRTSvcsag
VRTSvcsea
VRTSvxfen'
+ for ip in '`cat pk`'
++ ssh mokshi 'pkginfo |egrep "SUNWfmd|SUNWfsmgtr" |cut -d " " -f7 '
Password:
+ M='SUNWfmd
SUNWfmdr
SUNWfsmgtr'
+ echo 'mokshi '
+ echo 'SUNWfmd
SUNWfmdr
SUNWfsmgtr'
+ for j in '`cat ser`'
++ cat pk
+ for ip in '`cat pk`'
++ ssh niki 'pkginfo |egrep "VRTSvcs|VRTSvxfen" |cut -d " " -f7 '
Password:
+ M='VRTSvcs
VRTSvcsag
VRTSvcsea
VRTSvxfen'
+ echo 'niki '
+ echo 'VRTSvcs
VRTSvcsag
VRTSvcsea
VRTSvxfen'
+ for ip in '`cat pk`'
++ ssh niki 'pkginfo |egrep "SUNWfmd|SUNWfsmgtr" |cut -d " " -f7 '
Password:
+ M='SUNWfmd
SUNWfmdr
SUNWfsmgtr'
+ echo 'niki '
+ echo 'SUNWfmd
SUNWfmdr
SUNWfsmgtr'

我得到这样的输出:

bash-3.00# cat output

moksha

VRTSvcs
VRTSvcsag
VRTSvcsea
VRTSvxfen

moksha

SUNWfmd
SUNWfmdr
SUNWfsmgtr

niki

VRTSvcs
VRTSvcsag
VRTSvcsea
VRTSvxfen
niki

SUNWfmd
SUNWfmdr
SUNWfsmgtr

脚本的主要目标是它从服务器文件中获取一个服务器,并且它必须搜索包文件中的第一行。它必须从服务器名称中获取第二个服务器名称,并在包文件中搜索第二行。

请帮助我在哪里犯了错误。

【问题讨论】:

    标签: bash shell awk sed solaris


    【解决方案1】:

    您可以这样做 - 假设您的服务器和包文件中有相同数量的行:

    #!/usr/bin/env bash
    while read -u 3 -r server && read -u 4 -r pkg; do
      m=$(ssh -n "$server" "pkginfo | egrep '$pkg' | cut -d' ' -f7")
      echo "$server"
      echo "$m"
    done 3<ser 4<pk >> output
    
    • 3&lt;ser 将 fd 3 连接到文件 ser4&lt;pk 将 fd 4 连接到文件 pk
    • read -u 3 从文件描述符 3 读取,read -u 4 从文件描述符 4 读取
    • $() 比反引号更好(请参阅下面的帖子了解原因)
    • cut -d' ' -> 猜你的分隔符是空格
    • 需要ssh -n,以便ssh 忽略标准输入并且不会干扰read
    • 最好将&gt;&gt; output 放在末尾,以提高 I/O 效率

    另见:

    【讨论】:

    • 感谢您的帮助。我能知道这里的 3 和 4 是什么吗?下面的句子我也没有理解。什么是 fd 3
    • 查看这篇文章以了解文件描述符:stackoverflow.com/questions/5256599/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 2023-01-29
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    相关资源
    最近更新 更多