我正在尝试编写一个 bash 脚本来访问一个文件(nodeNames),该文件包含集群网络中不同计算机的 IP 地址,ssh
到这些计算机中的每一个并输出一些基本信息,即:主机名、主机 IP 地址、平均负载和使用最多内存的进程,并将所有这些信息附加到一个文件中,每个文件用逗号分隔。此外,每台计算机都有相同的用户和密码。到目前为止,这是我的代码,但它无法正常工作,我在这里需要帮助
egrep -ve '^#|^$'nodeNames | while read a
do
ssh $a "$@" &
output1=`hostname`
#This will display the server's IP address
output2=`hostname -i`
#This will output the server's load average
output3=`uptime | grep -oP '(?<=average:).*'| tr -d ','`
#This outputs memory Information
output4=`ps aux --sort=-%mem | awk 'NR<=1{print $0}'`
#This concantenates all output to a single line of text written to
echo "$output1, $output2, $output3, $output4" | tee clusterNodeInfo
done
最佳答案
你需要了解在哪台计算机上执行什么。你启动的 shell 脚本在你的主机 A 上执行,你想从你的主机 B 获取信息。 ssh $a "$@"&
不会突然让所有命令在远程主机 B 上执行。因此,
output1=`hostname`
将在主机 A 上执行,output1
将具有主机 A 的主机名。
您可能还想将 tee
放在循环之外或使用 tee -a
来防止覆盖您的输出文件。
对于 bash
,使用 $()
而不是 `` 。
那么,这将使您的脚本:
egrep -ve '^#|^$'nodeNames | while read a
do
output1=$(ssh $a hostname)
#This will display the server's IP address
output2=$(ssh $a hostname -i)
#This will output the server's load average
output3=$(ssh $a "uptime | grep -oP '(?<=average:).*'| tr -d ','")
#This outputs memory Information
output4=$(ssh $a "ps aux --sort=-%mem | awk 'NR<=1{print $0}'")
#This concantenates all output to a single line of text written to
echo "$output1, $output2, $output3, $output4" | tee -a clusterNodeInfo
done
(没有测试过,不过应该是这样的)
关于linux - 使用 bash 脚本检查网络中不同计算机的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50335620/