Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 11th, 2005, 8:42 PM   #1
erauqssidlroweht
Newbie
 
Join Date: Jul 2005
Posts: 2
Rep Power: 0 erauqssidlroweht is on a distinguished road
Bash Network Audit

I'm kinda a newbie to programming, so I'll apologize in advance. I'm trying to write a bash script that will go out and test my servers and make sure they are still happily working away. In the event the are not working as I think they should I want the script to call my pager. (Pager script works fine btw.)

I'm coming into problems in more ways than one. After I have connected to the remote service I can't figure out how to exit from the telnet session.
I.E. The mail server expects a response. HELO response. I just want to quit and grep the session. (See below)

Secondly I seem to be getting an EOF (unexpected end of file) error message after trying to execute this script. I've looked over the syntax and I am just so new to this I can’t see what I’ve done wrong.

Any help and guidance would be much appreciated.

-------Begin Script-------
#!/bin/sh

#List of Servers to Check
#Insert spaces to separate multiple entries
EMAIL="192.168.1.10"
DNS="192.168.1.5"
ROUTER="192.168.1.1 192.168.1.2"

#For Every Email value telnet to port 25 and make sure everything is normal
for SYS in $EMAIL; do CHECKMAIL=`telnet $SYS 25 | grep -o "Connected"`
if [ $CHECKMAIL -ne "Connected" ] ; then
/PAGER/911dial.sh
fi

#For every DNS server check to see if it can resolve www.google.com
for SYS2 in $DNS; do DNSLOOKUP=`dig @$SYS2 www.google.com | grep -o "answer"`
if [ $DNSLOOKUP -ne "answer" ] ; then
/PAGER/911dial.sh
fi

#Telnet to routers and double check they are active VIA Inband connection
for SYS3 in $ROUTER; do TELNET=`telnet $SYS3 | grep -o "Password:"`
if [ $TELNET -ne "Password:" ] ; then
/PAGER/911dial.sh
fi
done

#End of Critical Service Check
-------End Script-------
erauqssidlroweht is offline   Reply With Quote
Old Jul 12th, 2005, 11:57 AM   #2
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
You can create a function to do the connect and quit for telnet like this
UID="userid"
PWD="passwd"
ip_addr=""
extelnet ()  {
 awk '{ if(NR == 1) {   
        print UI
        print PW
        }
        print $0}
        END{print "quit"}' UI=$UID PW=$PSWD | \
        /usr/bin/telnet  | \
        grep  /^Connected/
 }
          
EMAIL="1.1.1.1  2.2.2.2.2  3.3.3.3"
for ip_addr in $EMAIL
do
    connected=`echo "ls" |  extelent`
    if [ connected -ne "Connected" ]; then
       echo "ip_addr=$ip_addr not connected"
    fi
done

I did not test this code, you need to check it.
jim mcnamara is offline   Reply With Quote
Old Jul 12th, 2005, 6:44 PM   #3
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 335
Rep Power: 4 mackenga is on a distinguished road
If you want to interact with network services like Telnet, life is a lot easier if you use expect. See http://expect.nist.gov/ for more information.

(To terminate your telnet session, I believe you usually send a control-closed-square-bracket or something like that, which should leave you talking to the telnet program rather than the mailserver; then you can send the quit command to the telnet client; don't hold me to that though!)
mackenga is offline   Reply With Quote
Old Jul 13th, 2005, 10:54 AM   #4
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
mack is right, except a lot of unixes do not have expect - linux usually does.
You can download it from the expect homepage:

http://expect.nist.gov/
jim mcnamara is offline   Reply With Quote
Old Jul 21st, 2005, 5:45 PM   #5
erauqssidlroweht
Newbie
 
Join Date: Jul 2005
Posts: 2
Rep Power: 0 erauqssidlroweht is on a distinguished road
I didn't have Expect installed so I went ahead installed and have been working with it for the week or so.

I've successfully writen a two part script. And later I expanded on this concept for every service I have.

#!/bin/sh
#Debian Linux
# Check if SMTP is up
/PAGER/smtp.exp 1>/dev/null 2>&1
if [ $? == 0 ]
then
echo "SMTP is running."
else
echo "SMTP is NOT running. Calling for help..."
/PAGER/911dial.sh
fi
#Exit


#!/usr/bin/expect -f
#The second part
set timeout 4
spawn telnet 192.168.1.10 25
expect "220 "
send "quit\r"


Thanks everyone for your help and direction.
erauqssidlroweht is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:37 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC