Simple script to test mountGpoint for read-write


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple script to test mountGpoint for read-write
# 1  
Old 02-20-2017
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]
Code:
[root@xxxx mnt]# touch test123
touch: cannot touch `test123': Read-only file system
root@xxxx mnt]#

3. An email get sent from ServerA to my personal email.

Thanks!


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by rbatte1; 02-21-2017 at 08:28 AM.. Reason: Added CODE tags.
# 2  
Old 02-20-2017
Hi,

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

Code:
#!/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.

Code:
$ 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.
This User Gave Thanks to drysdalk For This Post:
# 3  
Old 02-20-2017
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:

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

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 02-20-2017
Quote:
Originally Posted by drysdalk
Hi,

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

Code:
#!/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.

Code:
$ 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.
Yes thank you!!! script worked like a charm!!
# 5  
Old 02-20-2017
Hi,

Great - glad that worked for you !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need a UNIX/perl script to read and write the data

Hi, I have on Designdocument in that information is stored with in tabular format.I need Perl/unix script to read and write the data using perl script? Regards, Ravi (4 Replies)
Discussion started by: toravi.pentaho
4 Replies

2. Shell Programming and Scripting

Need a perl script to read and write the data

Hi, I have on Designdocument in that information is stored with in tabular format.I need Perlscript to read and write the datausing perl script? Regards, Ravi (0 Replies)
Discussion started by: toravi.pentaho
0 Replies

3. UNIX for Dummies Questions & Answers

Simple script to write new lines in a text file

Hello, I have a comma seperated data sheet with multiple fields of biological data. One column contains the ID name of the sample, where there could be more than one sample separated by a comma. I would like a script that reads this field, and for each sample ID, copies the entire line and writes... (18 Replies)
Discussion started by: torchij
18 Replies

4. Shell Programming and Scripting

Write shelll script to read file location

hi all i have a problem how to read file location..I read file as FILE=/home/tmp/new.file.but t is not useful for me.But i want my script read file location where the file is and copy in directory at boot time. Every time of booting files are copied in respective folder.please help !!!!:) (2 Replies)
Discussion started by: shubhig15
2 Replies

5. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

7. Shell Programming and Scripting

Bash Script to Read & Write on different directories

Hi, root@server] df -h 121G 14G 101G 12% /home 147G 126G 14G 91% /backup We having our site files and images are storing in /backup/home/user/files/ through symbolic link created in /home directory pointing in /backup directory as following. root@server] cd /home... (1 Reply)
Discussion started by: mirfan
1 Replies

8. Shell Programming and Scripting

i want to write a script to test the folder permissions

Hi All, I want a script to test folder permissions.( to alert me if a folder /abc/xyz does not have 775 permissions). i want to test /abc/xyz is having 775 permissions or not if not it has to alert me. Please help me ASAP Thanks in advance (4 Replies)
Discussion started by: rajesh212512
4 Replies

9. UNIX for Dummies Questions & Answers

help with simple read script!

Hi y'all...I've been wracking my brain over this very simple script that reads in a textfile and echos each line. But I keep getting an error. Below is my script: #!/bin/bash while read myline do echo $myline done < t_file-20080221.01.asc The error I am getting is: syntax error... (1 Reply)
Discussion started by: kevlar28
1 Replies

10. Shell Programming and Scripting

Script with read/write Files

Hello, I am a Newbie in ksh Unix Script. So I must write a ksh/sh script who read character at a position in a File. So also it must read all the lines who belongs at these characters , then write these lines in a another File. Can you help me , or give little councils to advance with my... (5 Replies)
Discussion started by: steiner
5 Replies
Login or Register to Ask a Question