![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 2
Rep Power: 0
![]() |
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------- |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
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
doneI did not test this code, you need to check it. |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 314
Rep Power: 4
![]() |
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!) |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
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/ |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jul 2005
Posts: 2
Rep Power: 0
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|