Find the answer to your Linux question:
Results 1 to 2 of 2
Hello all, This is my first post, not sure if this is the correct place in the forum but I hope you can help me. I've mainly worked with powershell ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2013

    Using grep to check, and perform a command based on that check

    Hello all,

    This is my first post, not sure if this is the correct place in the forum but I hope you can help me. I've mainly worked with powershell before so some of these questions may seem obvious or noobish so I'm sorry for that!

    So anyway, I'm looking to create a script that performs a check on drives on a remote server and based on what drives are up or down, runs another command to bring the down drives back up. I'm unsure of a couple of things, like using grep to check the status of the drive, and based on that status, perform a command. So the commands I think I need to use, in the order they are to be executed are as follows:

    Connect to server
    ssh login password server

    Display drive status
    sudo /usr/openv/volmgr/bin/vmoprcmd -dps

    This returns the following in the console:

    Drv DrivePath Status Label Ready
    0 /dev/nst11 UP Yes Yes
    1 /dev/nst3 UP Yes Yes
    2 /dev/nst2 UP Yes Yes
    3 /dev/nst14 UP Yes Yes
    4 /dev/nst8 UP Yes Yes
    5 /dev/nst16 UP Yes Yes
    6 /dev/nst21 UP Yes Yes
    7 /dev/nst0 UP Yes Yes
    8 /dev/nst1 UP - No
    9 /dev/nst22 UP Yes Yes
    10 /dev/nst20 UP Yes Yes
    11 /dev/nst15 UP - No
    12 /dev/nst9 UP Yes Yes
    13 /dev/nst6 UP Yes Yes
    14 /dev/nst5 UP Yes Yes
    15 /dev/nst4 UP Yes Yes
    16 /dev/nst18 UP Yes Yes
    17 /dev/nst23 UP - No
    18 /dev/nst19 UP Yes Yes
    19 /dev/nst12 UP Yes Yes
    20 /dev/nst7 DOWN Yes Yes
    21 /dev/nst13 UP Yes Yes
    22 /dev/nst17 UP - No
    23 /dev/nst10 UP - No

    From this output I hope to use grep to find the downed drive and run this command on what the grep finds, for instance seeing as 20 is down:

    sudo /usr/openv/volmgr/bin/vmoprcmd ľup 20 etc

    Is this possible? Is there a way to use grep and pipeline the results into that command?

    Also I'm confused about sudoing, when executing these commands manually I have to sudo which asks for a password. Is it possible to have a prompt for this or have the script memorize the password entered originally (when sshing to the server)?

    and Finally, use grep again to find if all drives are up and exit the server.

    Thanks for your help guys!


  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Hi and welcome

    There are some questions on why you go that route and if it is the best approach. More on that later.
    What you ask can be done like this:

    1) Grant the user dancronin passwordless sudo rights for two "volmgr" commands
    As root on the server
    cat <<EOF >/etc/sudoers.d/dancronin
    Cmnd_Alias VOLMGR = /usr/openv/volmgr/bin/vmoprcmd, /usr/openv/volmgr/bin/vmoprcm
    dancronin      ALL = NOPASSWD: VOLMGR
    chmod 0600 /etc/sudoers.d/dancronin
    2) Establish a passwordless and keybased ssh login from your(?) workstation to the server
    On the workstation, it is assumed, that the user is dancronin:
    <choose a good passphrase>
    ssh-copy-id YOUR_SERVER
    Check if it works:
    You will have to enter the passphrase once per shell session.
    For details read documentation on "ssh-agent"

    3) Create a script in an appropiate place and name it. Maybe "":
    This is a skeleton. There is some hardcoding, availability of the commands is not checked, no errorhandling, etc
    #!/usr/bin/env bash
    function check_drives {
      sudo /usr/openv/volmgr/bin/vmoprcmd -dps   | sed -nre 's#^([[:digit:]]+) /dev/nst.* DOWN .*$#\1#p'
    ## Check for drives in status DOWN and try to bring them UP
    for DRVID in $(check_drives); do
      echo "sudo /usr/openv/volmgr/bin/vmoprcmd -up $DRVID"
    ## Check for drives which are still down
    if [ -n "$DRVIDS" ]; then
      echo -e "Drives in DOWN state:\n$DRVIDS" 1>&2
      exit 1
    4) Call it

    As for the the concerns:
    a) The example above is done via the user dancronin.
    You might want to introduce a new user on the server with the sole purpose of handling this script.
    Aka: Create a service account, that is independend from yours.

    b) It seems, this script tries to repair a broken state.
    The question is: How do the drives go to DOWN state in the first place? Is this expected?
    If not, it would be better to fix the root cause than cover up the symptons.
    c) If it is expected, then you probably dont want to be bothered to call the script manually.
    I would suggest to report the output to a monitoring solution (for example by integrating this script in e.g. nagios)
    d) Or create a cronjob for root and send errors via mail
    - You need root rights anyway to call the vmo* commands
    - Only someone you already has root can establish a root cronjob
    - There is no need for the sudo setup
    You must always face the curtain with a bow.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts