LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-19-2007, 02:07 AM   #1
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Rep: Reputation: 17
Question 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........
 
Old 11-19-2007, 02:13 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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"

Last edited by acid_kewpie; 11-19-2007 at 02:17 AM.
 
Old 11-19-2007, 02:22 AM   #3
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by acid_kewpie View Post
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
 
Old 11-19-2007, 11:08 AM   #4
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Question 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...
 
Old 11-19-2007, 11:23 AM   #5
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Angry

I mean how we can make decision from grep...
 
Old 11-19-2007, 11:26 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
By testing it's result code ... it's 0 on success, 1 on failure.



Cheers,
Tink
 
Old 11-19-2007, 11:41 AM   #7
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Question

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
.
.
.
.
.
.
 
Old 11-19-2007, 11:48 AM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 11-19-2007, 01:07 PM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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
 
Old 11-19-2007, 01:31 PM   #10
Hewson
Member
 
Registered: Feb 2007
Location: /home
Distribution: Kubuntu and CentOS
Posts: 214

Rep: Reputation: 32
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!

Last edited by Hewson; 11-19-2007 at 01:33 PM.
 
Old 11-19-2007, 02:51 PM   #11
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well the point is expect is not a good tool for the job in the first place...
 
Old 11-19-2007, 02:57 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by acid_kewpie View Post
well the point is expect is not a good tool for the job in the first place...
and tcl is ugly as sin ;}
 
Old 11-19-2007, 03:25 PM   #13
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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...
 
Old 11-19-2007, 03:54 PM   #14
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Smile

#!/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.....

Last edited by senthilvael; 11-19-2007 at 04:04 PM.
 
Old 11-19-2007, 03:59 PM   #15
senthilvael
Member
 
Registered: Nov 2007
Posts: 73

Original Poster
Rep: Reputation: 17
Smile

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


Reply

Tags
expect, python, tcl



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LUKS automation script jippo Linux - Security 6 01-12-2008 12:56 AM
Automation Script !! Create automated archive of files based on name of file! yoshima Programming 5 09-28-2007 05:17 AM
Running Telnet in a script, want to save Telnet output, howtodothis??? anil3 Linux - Software 2 03-08-2006 04:01 PM
telnet script help GUIPenguin Linux - General 2 01-16-2005 02:48 PM
User automation Script Pigdog Linux - Networking 1 02-26-2003 02:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration