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!
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
MAIL1="email@email.com"
for servers in `cat /export/home/xxx/servers_all`
do
echo "$servers"
grep $servers /export/home/xxx/servers_exclude >/dev/null 2>/dev/null
rc=$?
if [ "$rc" = "0" ]
then
continue
fi
if [ "`/opt/xx/bin/snmpget $servers <MIB goes here> 2>&1 | grep 'No response'`" ];
then
/usr/bin/echo "SNMPD Down on $servers" |/usr/bin/mailx -s "SNMPD Down on $s
ervers" $MAIL1;
fi
done
#!/bin/bash
MAIL1="email@email.com"
IFS="
"
for servers in `cat /export/home/xxx/servers_all`;do
echo "$servers"
if [ `grep -c $servers /export/home/xxx/servers_exclude` -eq 0 ];then
echo OK.
fi
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;
fi
done
Last edited by david_ross; 02-27-2004 at 12:50 PM.
What's in the other file? Is server_all some sort of output file and server_exclude the file with all the correct host/ip combo's?
I think I know what you are trying to do, but there's not enough info. Give a few short examples, tell us which file holds what, are there many (uniq) hosts/ip combo's. Things like that.
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>
#!/bin/bash
MAIL1="email@email.com"
IFS=","
cat /export/home/xxx/servers_all | \
while read HOST_NAME HOST_IP
do
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 ]
then
# nope, poll host
echo "-----> code to poll $HOST_NAME goes here"
if [ "$?" -ne 0 ]
then
# Could not poll host, trying ip number
echo "-----> code to poll $HOST_IP goes here"
if [ "$?" -ne 0 ]
then
# Could not poll ip, mail this
echo "-----> code for mailing goes here"
fi
fi
fi
echo "checking next line"
done
The code does need cleaning/tailoring, but that's up to you :-)
I believe this only works if you change server all to space delimited. Otherwise the read takes all of the data for the HOST_NAME. That is if the format is:
host1,10.32.120.128
host2,10.32.120.120
Otherwise, if the format is
host1, 10.32.120.128
host2, 10.32.120.120
HOST_NAME gets host1,
HOST_IP gets 10.32.120.128
But if you just lose the comma altogether:
host1 10.32.120.128
host2 10.32.120.120
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.
Originally posted by druuna Found the problem. It's not the comma, you need that to seperate the fields in server_all in order to make the while read construct work..
You're right. At work I use an older version of bash and there the comma didn't work, but at home on my Linux box it works fine. Must have been a change in the definition of "whitespace" or something.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.