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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
11-19-2007, 02:07 AM
|
#1
|
Member
Registered: Nov 2007
Posts: 73
Rep:
|
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........
|
|
|
11-19-2007, 02:13 AM
|
#2
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
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.
|
|
|
11-19-2007, 02:22 AM
|
#3
|
Member
Registered: Nov 2007
Posts: 73
Original Poster
Rep:
|
Quote:
Originally Posted by acid_kewpie
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
|
|
|
11-19-2007, 11:08 AM
|
#4
|
Member
Registered: Nov 2007
Posts: 73
Original Poster
Rep:
|
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...
|
|
|
11-19-2007, 11:23 AM
|
#5
|
Member
Registered: Nov 2007
Posts: 73
Original Poster
Rep:
|
I mean how we can make decision from grep...
|
|
|
11-19-2007, 11:26 AM
|
#6
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
By testing it's result code ... it's 0 on success, 1 on failure.
Cheers,
Tink
|
|
|
11-19-2007, 11:41 AM
|
#7
|
Member
Registered: Nov 2007
Posts: 73
Original Poster
Rep:
|
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
.
.
.
.
.
.
|
|
|
11-19-2007, 11:48 AM
|
#8
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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
|
|
|
11-19-2007, 01:07 PM
|
#9
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
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
|
|
|
11-19-2007, 01:31 PM
|
#10
|
Member
Registered: Feb 2007
Location: /home
Distribution: Kubuntu and CentOS
Posts: 214
Rep:
|
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.
|
|
|
11-19-2007, 02:51 PM
|
#11
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
well the point is expect is not a good tool for the job in the first place...
|
|
|
11-19-2007, 02:57 PM
|
#12
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally Posted by acid_kewpie
well the point is expect is not a good tool for the job in the first place...
|
and tcl is ugly as sin ;}
|
|
|
11-19-2007, 03:25 PM
|
#13
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
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...
|
|
|
11-19-2007, 03:54 PM
|
#14
|
Member
Registered: Nov 2007
Posts: 73
Original Poster
Rep:
|
#!/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.
|
|
|
11-19-2007, 03:59 PM
|
#15
|
Member
Registered: Nov 2007
Posts: 73
Original Poster
Rep:
|
Chris, your idea is perfect i should not use that text file.....
Really great..
|
|
|
All times are GMT -5. The time now is 11:00 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|