• Uncategorized

About linux : Shell-Script-to-Check-if-Linux-Storage-is-Accessible-on-Linux-Host

Question Detail

I have a list of Linux hosts and I want do a script that will check if a a bunch of Linux storages are mounted and accessible on each of these hosts and then echo if the storage was not accissble and on which host. The best way I can think of doing this is using pdsh -w ^hostslist "command" so my script will look something like this:


storages="s1 s2 s3 s4" 

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 

Then in the same script, check if it the file output I got is empty, then read it and grep for “no such file or directory” and if the error message was found, then echo that the directory doesn’t exist on that host. So it will be something like this

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

But I am not sure if this is the best approach to check if a storage is accessible on a certain host. Is this the best approach, or is there a better way of doing this?

Question Answer

Since you have a list of hosts and seems that you want to do a task in all of them by using pdsh, I would suggest giving a try to ansible

A very basic implementation would be to first create your inventory create a file named my-inventory with your real addresses, for example:


Then to check only if a file exists you could create this basic playbook a file check-if-storage-exists.yml with this contents:

- hosts: my-hosts

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

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

Then give a try like this:

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

It may look a little overkilling but it may worth give a try since you could do much more things, for example, you could use the mount module, etc.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.