Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mail script

Status
Not open for further replies.

grapist

Technical User
Jan 11, 2010
6
0
0
US
Hey I'm new to scripting and I wanted to create a bash script that would first ping all the nodes at my work and then try to ssh into all the nodes and list if the commands were successful or not into a file which then gets emailed to me and other important people at work. This is what I have so far;
#!/bin/bash

####################
# Config
####################

# Client Host list
nodes=`cat host_list`
date= `date`
for node in ${nodes}; do

echo -n "Attempting to reach $node..."
ping -c 3 $node > /dev/null
if [ $? -ne 0 ]; then
echo "$node not alive" >> /home/alex/node_status$date.txt
else
# do ssh here
echo "$node on-line" >> /home/alex/node_status$date.txt
fi

done
Thanks in advanced for any suggestions.
Alex
 
Looks fine so far, except for one thing... the default output of date has spaces in it which aren't ideal for the output filename, so you may prefer to use date +%Y%m%d or similar.

Annihilannic.
 
Hi

And see if your [tt]ping[/tt] has -w option. According to my experience, some pinging attempts may wait for almost an eternity on failure. The -w is the only way ( well, the only simple way ) I found to avoid such hanging :
man ping said:
-w deadline
Specify a timeout, in seconds, before ping exits regardless of
how many packets have been sent or received. In this case ping
does not stop after count packet are sent, it waits either for
deadline expire or until count probes are answered or for some
error notification from network.


Feherke.
 
And you need to lose the space after the equals sign on "[tt]date=[/tt]" also...
[tt]
nodes=`cat host_list`
date= `date`
^[/tt]
will become something like:
[tt]
nodes=`cat host_list`
date=`date +%'Y%m%d_%H%M'`[/tt]


Here's an off-topic tip:

In future, if you put your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] (or between [tt][ignore][tt][/ignore][/tt] and [tt][ignore][/tt][/ignore][/tt] like I used above), your code text will appear in a fixed-width font and that makes it easier to read it and to spot errors like this.

See the [link javascript:eek:penindex(450,450,']Process TGML[/url] link on the bottom of the "Edit Post" box for more information.

HTH,

p5wizard
 
I'm having most of the trouble with the email. Should I write a separate script to send the email?
 
Hi

grapist said:
Should I write a separate script to send the email?
Why ? Sending a mail is just one command :
Code:
[gray]# send the file in the mail body[/gray]
mail -s 'ping results' one.boss@example.com other.boss@example.com < /home/alex/node_status$date.txt

[gray]# send the file as attachment[/gray]
mutt -s 'ping results' -a /home/alex/node_status$date.txt one.boss@example.com other.boss@example.com <<ENDOFBODY
here is attached the latest ping log
ENDOFBODY

Feherke.
 
I keep getting a permission denied error every time I use the first command
this is what I keep seeing;
/tmp/Rs7FGYPI: Permission denied
 
I also think it would work better with two scripts because with it all in one it sends email after every ping instead of one file with all the pings.
 
Hi

grapist said:
I keep getting a permission denied error every time I use the first command
this is what I keep seeing;
/tmp/Rs7FGYPI: Permission denied
Well, that is not scripting problem, but system administration. Check the ownership and permissions of the /tmp/ directory. A [tt]strace[/tt] output could give details on the failed operation.
grapist said:
I also think it would work better with two scripts because with it all in one it sends email after every ping instead of one file with all the pings.
That is plain crap. There is no difference from this point between executing a command and executing a script with a command. Anyway, I meant to put the mail sending command after the loop :
Bash:
[navy]nodes[/navy][teal]=[/teal]`cat host_list`
[navy]date[/navy][teal]=[/teal]`date [teal]+%[/teal][green][i]'Y%m%d_%H%M'[/i][/green]`

[b]for[/b] node [b]in[/b] [navy]${nodes}[/navy][teal];[/teal] [b]do[/b]

        echo -n [green][i]"Attempting to reach $node..."[/i][/green]
        ping -c [purple]3[/purple] [navy]$node[/navy] [teal]>[/teal] /dev/null
        [b]if[/b] [teal][[/teal] [navy]$?[/navy] -ne [purple]0[/purple] [teal]];[/teal] [b]then[/b]
                echo [green][i]"$node not alive"[/i][/green] [teal]>>[/teal] /home/alex/node_status[navy]$date[/navy][teal].[/teal]txt
        [b]else[/b]
                [gray]# do ssh here[/gray]
                echo [green][i]"$node on-line"[/i][/green] [teal]>>[/teal] /home/alex/node_status[navy]$date[/navy][teal].[/teal]txt
        fi

[b]done[/b]

mutt -s [green][i]'ping results'[/i][/green] -a /home/alex/node_status[navy]$date[/navy][teal].[/teal]txt one[teal].[/teal]boss@example[teal].[/teal]com other[teal].[/teal]boss@example[teal].[/teal]com [teal]<<[/teal]ENDOFBODY
here is attached the latest ping log
ENDOFBODY

Feherke.
 
Thanks for all your help Feherke, I switched to a machine where I have root permissions and it worked almost better than perfect.
Thanks again
Alex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top