LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Telnet automation script (https://www.linuxquestions.org/questions/linux-newbie-8/telnet-automation-script-600753/)

senthilvael 11-19-2007 02:07 AM

Telnet automation script
 
Hi
my work is server monitoring. Simply by using telnet i will check particular server's port is open or not.This is a primary test.for example,

telnet 1.2.3.4 25(port 25)

I have to check more than 20 servers frequently.
I wrote a script simply which automated my work partially.

But if we use 'expect' we can automate this fully. But i cant get much help about 'expect'. Can anyone help me in completing this script? Please........

acid_kewpie 11-19-2007 02:13 AM

clearly not an intro. moved to newbie.

as for your issue, telnet is fine for manual checking, but not for automating it really, as you can't nicely control what telnet does. Expect is for controlling the flow of otherwise manual commands, but there are no manual commands in this test. instead just use a tool like nmap - "nmap -P0 -p25 1.2.3.4"

senthilvael 11-19-2007 02:22 AM

Quote:

Originally Posted by acid_kewpie (Post 2963769)
clearly not an intro. moved to newbie.

as for your issue, telnet is fine for manual checking, but not for automating it really, as you can't nicely control what telnet does. Expect is for controlling the flow of otherwise manual commands, but there are no manual commands in this test. instead just use a tool like nmap - "nmap -P0 -p25 1.2.3.4"

Thank u very much sir for this fast reply. now i am in home(no linux mechine)Tonight i will check n try to finish the script using nmap. I wont forget your help. Thanks again

senthilvael 11-19-2007 11:08 AM

Again some more help.....
 
As per your advise i am going to use 'nmap -P0 -p25 1.2.3.4' in my script.
my script will b in the form of..


#!/bin/bash
flag=0 #i am introducing a flag
nmap -P0 -p110 1.2.3.4 > test.txt #directing op to a file
grep open test.txt #checking this file is having a
word 'open'
#if yes, no problem can go to
next server check dont modify flag
#Else have to create an error msg
n goto next server n make flag to 1



This is my logic. How can i use if. I mean how to give condition? Please help me...

senthilvael 11-19-2007 11:23 AM

I mean how we can make decision from grep...

Tinkster 11-19-2007 11:26 AM

By testing it's result code ... it's 0 on success, 1 on failure.



Cheers,
Tink

senthilvael 11-19-2007 11:41 AM

How to check d result code sir. Can u please give me an example. or can we try like this.....

nmap -P0 -p110 1.2.3.4 > test.txt
if [ grep -c open test.txt -eq 1 ] ; then
goto next

else
create error
.
.
.
.
.
.

Tinkster 11-19-2007 11:48 AM

Code:

if `grep -c open test.txt >/dev/null 2>&1` then
That's one option. The other possibilities are to check $? for
0 or to catpure the output of the grep in a variable and check
that ... always more than one way to do it. ;}

http://www.tldp.org/LDP/abs/html/index.html


Cheers,
Tink

acid_kewpie 11-19-2007 01:07 PM

you'd do well to eliminate the text file, it's totally redundant...

if [ "$(nmap -P0 -p110 1.2.3.4 | grep open)" ]
then
...
fi

Hewson 11-19-2007 01:31 PM

In the future, you may find using TCL (or Python) to do the variable testing is a little easier than using BASH.

Classic Expect is built on top of TCL. There are many resources for TCL.

There is a singular book on Expect itself Exploring Expect [google.com].

I spend a great deal of time automating. I am not a fan of TCL. Thankfully Noah Spurrier has blessed us with Pexpect [sourceforge.net], a pure python expect package for Python.

Happy Times!

acid_kewpie 11-19-2007 02:51 PM

well the point is expect is not a good tool for the job in the first place...

Tinkster 11-19-2007 02:57 PM

Quote:

Originally Posted by acid_kewpie (Post 2964497)
well the point is expect is not a good tool for the job in the first place...

and tcl is ugly as sin ;}

acid_kewpie 11-19-2007 03:25 PM

well when tcl is the only tool you have it *can* be kinda cute. I end up doing a reasonable amount of tcl within our F5 load balancers, and the more i use it the less i totally hate it...

senthilvael 11-19-2007 03:54 PM

#!/bin/bash
clear
sleep 1
nmap -P0 -p25 1.2.3.4 > test.txt
if grep -q open test.txt; then
echo SMTP is ok in 1.2.3.4
else
echo SMTP problem in 1.2.3.4
fi
sleep 1
nmap -P0 -p110 1.2.3.4 > test.txt
if grep -q open test.txt; then
echo POP is ok in 1.2.3.4
else
echo POP problem in 1.2.3.4
fi
.........................................................................

Finally With your valuable help, I finished like this. Now we can check for even 1000 servers.

My next step is to send a mail to a person if particular port is not open in any server. Can we do that from command line itself? I think we have to add in else part.....

senthilvael 11-19-2007 03:59 PM

Chris, your idea is perfect i should not use that text file.....
Really great..

senthilvael 11-19-2007 04:38 PM

#!/bin/bash
clear
sleep 1
if [ "$(nmap -P0 -p25 1.2.3.4 | grep open)" ]
then
echo SMTP is ok in 1.2.3.4
else
echo SMTP problem in 1.2.3.4
fi
sleep 1
if [ "$(nmap -P0 -p110 1.2.3.4 | grep open)" ]
then
echo POP is ok in 1.2.3.4
else
echo POP problem in 1.2.3.4
fi
...........................................................................
I checked. Its working perfectly.
Thank you Chris,Tink,Hewson


All times are GMT -5. The time now is 07:08 PM.