gpt4 book ai didi

linux - 用于检查 Linux 主机上是否可访问 Linux 存储的 Shell 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:38 25 4
gpt4 key购买 nike

我有一个 Linux 主机列表,我想做一个脚本来检查是否在每个主机上安装了一堆 Linux 存储并且可以访问,然后在存储不可访问时回显以及在哪个主机上。我能想到的最好的方法是使用 pdsh -w ^hostslist "command" 所以我的脚本看起来像这样:

#!/bin/sh

storages="s1 s2 s3 s4"
host=`hostname`

for storage in ${storages}; do
pdsh -w ^hosts "cd /$storage" 2 > &1 #here I want to save the output of what I get from running this command

然后在同一个脚本中,检查我得到的文件输出是否为空,然后读取它并 grep 查找“没有这样的文件或目录”,如果找到错误消息,则回显该目录不存在在那个主机上。所以它会是这样的

if [ -s 1 ] 
then
error=`cat 1 | grep "no such file or direcotry`
if [ -n $error]
then
echo "there is something wrong with $storage in $host"
fi
fi

但我不确定这是否是检查特定主机上是否可访问存储的最佳方法。这是最好的方法,还是有更好的方法?

最佳答案

由于您有一个主机列表,并且您似乎想使用 pdsh 在所有主机中执行一项任务。 , 我建议尝试 ansible

一个非常基本的实现是首先创建您的 inventory使用您的真实地址创建一个名为 my-inventory 的文件,例如:

[my-hosts]
10.10.1.1
10.10.1.2
10.10.1.3
10.10.1.4

然后为了仅检查文件是否存在,您可以创建这个基本的 playbook包含以下内容的文件 check-if-storage-exists.yml:

---
- hosts: my-hosts

tasks:
- name: Check if file exists
stat:
path: /storage
register: st

- debug:
msg: "path /storage doesn't exist"
when: not st.stat.exists

然后像这样尝试一下:

ansible-playbook -i my-inventory check-if-storage-exists.yml

它可能看起来有点矫枉过正,但值得一试,因为您可以做更多的事情,例如,您可以使用 mount module

关于linux - 用于检查 Linux 主机上是否可访问 Linux 存储的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49406864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com