Script template for inputting filenames and print results

Hi,

Hope you are all well. New to scripting, and all those characters are all a new language for me. Though hoping to get my little head round it all sooner or later.

I was wondering whether anyone could help with a script template example.

What I would like to happen is to run the script and for it to respond with whatever file I would like the script to check and then perform an md5sum check on it and then print out the results.

The thing is, that all hosts are on the same LAN but requires ssh into it.

Would appreciate any help given so I can start to get my head round this scripting marlakey.

Thanks in advance.

---------- Post updated at 11:05 AM ---------- Previous update was at 11:03 AM ----------

Oh and it's Linux RH5 if that helps :slight_smile:

#!/bin/bash

(( $# < 1 )) && {
  printf "usage: %s <file_name>\n" "${0##*/}"
  exit 1
  }

_server_list=(
  server1
  server2
  )
  
_fn=$1

_cmd=/usr/bin/md5sum

for s in "${_server_list[@]}"; do
  ssh "$s" "
    [ -f "$_fn" ] && 
      $_cmd $_fn" ||
        printf "error executing %s %s on %s\n" \
          "$_cmd" "$_fn" "$s"
done

Some of the double quotes might need escaping in the ssh command above.

Here is a version with a simple menu which you might find usefull examples of:

  • Select statement with validation and additional quit option
  • $(( )) calculations
  • Fetch number of elements in an array using ${#array[@]}
  • If then else clause
#!/bin/bash
(( $# < 1 )) && {
    printf "usage: %s <file_name>\n" "${0##*/}"
    exit 1
}
 
_server_list=( server1 server2 )
 
_fn=$1
_cmd=/usr/bin/md5sum
 
select s in "${_server_list[@]}" "quit"; do
    if [[ -z "$s" ]]
    then
        echo "Option $REPLY is invalid - Please select a number from 1 to $(( ${#_server_list[@]} + 1))"
    else
        [[ "$s" = "quit" ]] && break
        ssh "$s" "
            [ -f \"$_fn\" ] && 
                $_cmd $_fn ||
                printf \"error executing %s %s on %s\n\" \\
\"$_cmd\" \"$_fn\" \"$s\""
    fi
done

Yes, thanks!
Actually, more quotes are needed:

_cmd="[ -f '$_fn' ] && /usr/bin/md5sum '$_fn'"


for s in "${_server_list[@]}"; do
  ssh "$s" "$_cmd" ||
        printf  >&2 "error executing %s %s on %s\n" \
                  "$_cmd" "$_fn" "$s"
done

My god, yep it does look all weird to me :slight_smile:

Thanks for the input guys, really appreciate it. I will try this out on my test system and let you know the outcome. Hope it all goes well.

Again, thank you.

---------- Post updated at 04:37 AM ---------- Previous update was at 03:41 AM ----------

WOW, thanks, just tested it and it's just what I needed.

I maybe pushing the boat out a little bit further, what should I put in the file if I wanted an option to use all the servers in the server list as well as currently inputting one server at a time.

Thank you though guys. You really helped me out here.

Assuming the server list is a file in the following format:

server1
server2
...

You could use something like this:

_slist=<your_filename>

while IFS= read -r; do
  ssh "$REPLY" "$_cmd" ||
        printf "error executing %s %s on %s\n" \
                  "$_cmd" "$_fn" "$REPLY"
done < "$_slist"

Or, of course, just place all your servers in the _server_list array variable.

Hi guys,

I've noticed that the output is shown twice and a message is given, is there a reason why that is:

#? 1
96fbd1ef56e61e8455f356614516b829 /opt/xxx/lib/libhta.so
96fbd1ef56e61e8455f356614516b829 /opt/xxx/lib/libhta.so
/usr/bin/md5sum: server02: No such file or directory

This gives you a new menu item "All the above" (changes in green).

#!/bin/bash
(( $# < 1 )) && {
    printf "usage: %s <file_name>\n" "${0##*/}"
    exit 1
}
 
_server_list=( server1 server2 )
 
_fn=$1
_cmd=/usr/bin/md5sum
 
function dossh
{
ssh "$1" "
   [ -f \"$_fn\" ] && 
      $_cmd $_fn ||
      printf \"error executing %s %s on %s\n\" \\
\"$_cmd\" \"$_fn\" \"$1\""
}
 
select s in "${_server_list[@]}" "All the above" "quit"; do
    if [[ -z "$s" ]]
    then
        echo "Option $REPLY is invalid - Please select a number from 1 to $(( ${#_server_list[@]} + 2))"
    else
      [[ "$s" = "quit" ]] && break
      if [[ "$s" == All* ]]
      then
            for s in "${_server_list[@]}"
            do
                dossh $s
            done
            continue
      fi
      dossh $s
    fi
done

Just tried it. Didn't realise that there would be whole lot more to be added. Pardon my lack of understanding.

Thanks, that worked a treat. Really appreciate everyone who has helped me out here.

Thank you.