delimited and if
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 |
I haven't tried but what about:
Code:
#!/bin/bash |
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> Thanks so much for any direction you can give me! |
Something like this works:
Code:
#!/bin/bash |
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 then the given code works. |
sorry, code druuna gave me?
Thanks btw....I'm reworking it and will test it soon |
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.
|
thanks a bunch...I'm going to tinker around with it, think I'm pretty close...but it's almost quittin time!!
I'll let you know monday how it worked out. |
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..
I checked the wrong exit code: This line: if [ "$?" -eq 0 ] Should be if [ "$?" -ne 0 ] Sorry about that........... Just to make sure we have the same input files, I tested with these: $ cat server_all aaa,1.2.3.4 bbb,2.3.4.5 ccc,3.4.5.6 $ cat server_exclude aaa,3.4.5.6 This being the result after running the script: aaa 1.2.3.4 checking next line bbb 2.3.4.5 -----> code to poll bbb goes here checking next line ccc 3.4.5.6 checking next line First en third entry from servers_all should not be polled (are in server_exclude, one with hostname the other with ip). |
Quote:
I'll have to check that out. :study: |
just ran it, and all it looks to be working swelll....thanks so much to all of you fellas1!
|
All times are GMT -5. The time now is 06:01 PM. |