Simple script to test mountGpoint for read-write

Guys, need your help urgently.

  • ServerA
  • ServerB

ServerA has an email facility and can connect remotely via root using key to ServerB

What I am trying to achieve here is ServerA would be able to remotely:

  1. Do command ' touch /mnt/testfile.date ' on ServerB

  2. If touch failed with error below
    [/list]

[root@xxxx mnt]# touch test123
touch: cannot touch `test123': Read-only file system
root@xxxx mnt]#
  1. An email get sent from ServerA to my personal email.

Thanks!

Hi,

A script like this set up on ServerA should do the trick.

#!/bin/bash
date=`/bin/date +%Y%m%d%H%M%S`

email_to="unixforum@localhost"
email_subject="Something went wrong"
email_body="Uh-oh"

server="localhost"

if ! /usr/bin/ssh "$server" "/usr/bin/touch /mnt/testfile.$date" >/dev/null 2>/dev/null
then
        echo "$email_body" | /usr/bin/mail -s "$email_subject" "$email_to"
        exit 1
fi

exit 0

The 'date' variable at the top just creates a string via the 'date' command that consists of the year, month, date, hour, minute and second all run together. I went with this since you didn't specify any particular format, but this should do for most purposes (if the script only runs at most once per second, anyway).

The next three variables define the recipient, subject and body of the e-mail message. You can customise those all you like.

Finally, the variable 'server' defines the remote server that the 'ssh' command will use to connect to. You'll want to change this to the details of whatever ServerB is.

The basic idea of the script is that it connects to the remote server, and if the 'touch' command (or the 'ssh' command itself) returns a non-successful exit value for any reaason, it then proceeds to send the e-mail.

Here's an example session of it running, in a situation where the touch command at the remote end (which is really just 'localhost' in my test case) fails.

$ whoami
unixforum
$ ./script.sh 
$ mail
"/var/mail/unixforum": 1 message 1 new
>N   1 unixforum@localhost Mon Feb 20 15:17  15/654   Something went wrong
? 1
<e-mail headers appear here, redacted>

Uh-oh
? d 1
? q
Held 0 messages in /var/mail/unixforum
$ 

Hope this helps.

1 Like

Just a minor addition: i'd call the ssh-facility this way to prevent problems in case keys are changed/deleted. Not allocating a terminal ( -n ) prevents problems when used inside pipelines:

/usr/bin/ssh -nqo 'BatchMode = yes' "$server" "/usr/bin/touch /mnt/testfile.$date" >/dev/null 2>/dev/null

I hope this helps.

bakunin

1 Like

Yes thank you!!! script worked like a charm!!

Hi,

Great - glad that worked for you !