Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I have two files (servers_all and servers_exclude) where each line has hostname and ip, delimited by comma, and I want to write a shell script that will check the hostname in servers_all, and if that fails, will try the IP. And failure of IP will result in an email sent with hostname and IP. Frankly I am stuck. Does anyone have any ideas?> Below is code
for servers in `cat /export/home/xxx/servers_all`
grep $servers /export/home/xxx/servers_exclude >/dev/null 2>/dev/null
if [ "$rc" = "0" ]
if [ "`/opt/xx/bin/snmpget $servers <MIB goes here> 2>&1 | grep 'No response'`" ];
/usr/bin/echo "SNMPD Down on $servers" |/usr/bin/mailx -s "SNMPD Down on $s
for servers in `cat /export/home/xxx/servers_all`;do
if [ `grep -c $servers /export/home/xxx/servers_exclude` -eq 0 ];then
if [ `/opt/xx/bin/snmpget $servers <MIB goes here> 2>&1 | grep -c 'No response'` -gt 0 ]; then
/usr/bin/echo "SNMPD Down on $servers" |/usr/bin/mailx -s "SNMPD Down on $servers" $MAIL1;
Last edited by david_ross; 02-27-2004 at 01:50 PM.
Servers_all: comma delimited file containing hostname, and corresponding IP
Servers_exclude: list of hostnames, ips that we are not looking to poll, because we know there are problems. It's currently comma delimited, but it does not have to be obviously.
What I want to do is:
1) Goto first line and grab hostname, check to see if it exists in servers_exclude
2) If not, try to poll it for a response. If poll succeeds, move onto next line
3) If no response, poll the corresponding IP (if it does not exist in servers_exclude)
4) If no response to IP, send message stating that 'SNMPD Down on <hostname> <IP>
cat /export/home/xxx/servers_all | \
while read HOST_NAME HOST_IP
echo $HOST_NAME $HOST_IP
# Is entry present in servers_exclude
egrep "$HOST_NAME|$HOST_IP" /export/home/xxx/servers_exclude > /dev/null 2>&1
if [ "$?" -eq 0 ]
# nope, poll host
echo "-----> code to poll $HOST_NAME goes here"
if [ "$?" -ne 0 ]
# Could not poll host, trying ip number
echo "-----> code to poll $HOST_IP goes here"
if [ "$?" -ne 0 ]
# Could not poll ip, mail this
echo "-----> code for mailing goes here"
echo "checking next line"
The code does need cleaning/tailoring, but that's up to you :-)
Sorry, yeah the code druuna gave you. It's good code, just doesn't take into account the comma. Getting rid of the comma is pretty straight forward in perl or awk or sed. So, if you need it comma delimited let me know.